19 using System.Collections.Generic;
22 using Newtonsoft.Json;
23 using Newtonsoft.Json.Linq;
32 public string Name {
get;
set; }
42 public string Doc {
get;
set; }
47 public IList<Schema>
Types {
get;
set; }
52 public IDictionary<string,Message>
Messages {
get;
set; }
62 md5 = System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(
ToString()));
81 string doc, IEnumerable<Schema> types,
82 IDictionary<string,Message> messages)
84 if (
string.IsNullOrEmpty(name))
throw new ArgumentNullException(
"name",
"name cannot be null.");
85 if (
null == types)
throw new ArgumentNullException(
"types",
"types cannot be null.");
86 if (
null == messages)
throw new ArgumentNullException(
"messages",
"messages cannot be null.");
91 this.
Types =
new List<Schema>(types);
92 this.
Messages =
new Dictionary<string, Message>(messages);
102 if (
string.IsNullOrEmpty(jstring))
throw new ArgumentNullException(
"json",
"json cannot be null.");
107 jtok = JObject.Parse(jstring);
129 JToken jtypes = jtok[
"types"];
130 var types =
new List<Schema>();
131 if (jtypes is JArray)
133 foreach (JToken jtype
in jtypes)
135 var schema =
Schema.ParseJson(jtype, names, space);
140 var messages =
new Dictionary<string,Message>();
141 JToken jmessages = jtok[
"messages"];
142 if (
null != jmessages)
144 foreach (JProperty jmessage
in jmessages)
146 var message = Message.Parse(jmessage, names, space);
147 messages.Add(message.Name, message);
151 return new Protocol(name, space, doc, types, messages);
160 using (System.IO.StringWriter sw =
new System.IO.StringWriter())
162 using (Newtonsoft.Json.JsonTextWriter writer =
new Newtonsoft.Json.JsonTextWriter(sw))
165 writer.Formatting = Newtonsoft.Json.Formatting.Indented;
170 return sw.ToString();
180 internal void WriteJson(Newtonsoft.Json.JsonTextWriter writer,
SchemaNames names)
182 writer.WriteStartObject();
188 writer.WritePropertyName(
"types");
189 writer.WriteStartArray();
194 writer.WriteEndArray();
196 writer.WritePropertyName(
"messages");
197 writer.WriteStartObject();
199 foreach (KeyValuePair<string,Message> message
in this.
Messages)
201 writer.WritePropertyName(message.Key);
202 message.Value.writeJson(writer, names, this.
Namespace);
205 writer.WriteEndObject();
206 writer.WriteEndObject();
216 if (obj ==
this)
return true;
217 if (!(obj is
Protocol))
return false;
232 private bool TypesEquals(IList<Schema> that)
234 if (
Types.Count != that.Count)
return false;
236 if (!that.Contains(schema))
return false;
247 private bool MessagesEquals(IDictionary<string, Message> that)
249 if (
Messages.Count != that.Count)
return false;
250 foreach (KeyValuePair<string, Message> pair
in Messages)
252 if (!that.ContainsKey(pair.Key))
254 if (!pair.Value.Equals(that[pair.Key]))
267 GetTypesHashCode() + GetMessagesHashCode();
274 private int GetTypesHashCode()
276 int hash =
Types.Count;
286 private int GetMessagesHashCode()
289 foreach (KeyValuePair<string, Message> pair
in Messages)
290 hash += (pair.Key.GetHashCode() + pair.Value.GetHashCode());
override int GetHashCode()
Hash code function
IList< Schema > Types
List of schemas objects representing the different schemas defined under the 'types' attribute
Base class for all schema types
string Doc
Documentation for the protocol
string Name
Name of the protocol
static string GetRequiredString(JToken jtok, string field)
Retrieves the required string property value for the given property name from the given JSON object.
virtual internal void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes schema object in JSON format
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
A class that contains a list of named schemas.
static string GetOptionalString(JToken jtok, string field)
Retrieves the optional string property value for the given property name from the given JSON object.
override int GetHashCode()
Returns the hash code of this protocol object
override string ToString()
Writes Protocol in JSON format
static Protocol Parse(string jstring)
Parses the given JSON string to create a Protocol object
Protocol(string name, string space, string doc, IEnumerable< Schema > types, IDictionary< string, Message > messages)
Constructor for Protocol class
IDictionary< string, Message > Messages
List of message objects representing the different schemas defined under the 'messages' attribute
string Namespace
Namespace of the protocol
override bool Equals(object obj)
Tests equality of this protocol object with the passed object