コード例 #1
0
        /// <summary>
        /// Serializes the specified DDNode and writes the Json document to a Json writer
        /// </summary>
        /// <param name="n">the node to serialize</param>
        /// <param name="writer">Json writer used to write the Json document.</param>
        public static void Serialize(this DDNode n, JsonWriter writer)
        {
            writer.Formatting = Newtonsoft.Json.Formatting.Indented;
            writer.WriteStartObject();

            if (n.Name != null)
            {
                writer.WritePropertyName(n.Name);
            }
            writer.WriteStartObject();
            if (String.IsNullOrEmpty(n.Type) == false)
            {
                writer.WritePropertyName(DDSchema.JSON_SERIALIZE_ATTRIBUTE_TYPE);
                writer.WriteValue(n.Type);
            }
            if (n.Attributes.Count > 0)
            {
                DDAttributesCollectionSje.JsonSerialize(n.Attributes, writer);
            }
            if (n.HasChildNodes)
            {
                writer.WritePropertyName(DDSchema.JSON_SERIALIZE_NODE);
                writer.WriteStartArray();
                foreach (var keyValuePair in n)
                {
                    if (keyValuePair.Value != null)
                    {
                        keyValuePair.Value.Serialize(writer);
                    }
                }
                writer.WriteEndArray();
            }
            writer.WriteEndObject();
            writer.WriteEndObject();
        }
コード例 #2
0
 /// <summary>
 ///  Generates an new DDAttributesCollection from its Json representation.
 /// </summary>
 /// <param name="reader">Json stream reader</param>
 /// <returns>an new DDAttributesCollection</returns>
 public void Deserialize(JsonReader reader)
 {
     this.ac = DDAttributesCollectionSje.Deserialize(reader);
 }
コード例 #3
0
 /// <summary>
 ///  Generates an new DDAttributesCollection from its Json representation.
 /// </summary>
 /// <param name="s">Stream that contains the Json document to deserialize.</param>
 /// <returns>an new DDAttributesCollection </returns>
 public void Deserialize(Stream s)
 {
     this.ac = DDAttributesCollectionSje.Deserialize(s);
 }
コード例 #4
0
 /// <summary>
 /// Generates an new DDAttributesCollection from its Json representation.
 /// </summary>
 /// <param name="tr">Text reader stream that contains the Json document to deserialize.</param>
 public void Deserialize(TextReader tr)
 {
     DDAttributesCollectionSje.Deserialize(this.ac, tr);
 }
コード例 #5
0
        /// <summary>
        /// Generates an new DDNode from its Json representation.
        /// </summary>
        /// <param name="reader">Json stream reader</param>
        /// <returns>an new DDNode</returns>
        public static DDNode Deserialize(JsonReader reader)
        {
            DDNode n = null;
            string prevValueString = null;
            string prevName        = null;

            JsonToken prevTokenType = JsonToken.None;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                if ((reader.TokenType == JsonToken.PropertyName) && (n == null) && (reader.Value != null))
                {
                    n = new DDNode(reader.Value.ToString());
                }

                if ((reader.TokenType == JsonToken.String) && (n != null) && (prevTokenType == JsonToken.PropertyName) && (prevName == DDSchema.JSON_SERIALIZE_ATTRIBUTE_TYPE) && (reader.Value != null))
                {
                    n.Type = reader.Value.ToString();
                }

                if ((reader.TokenType == JsonToken.StartArray) && (prevName == DDSchema.JSON_SERIALIZE_NODE_ATTRIBUTE_COLLECTION) && (n != null))  // attributes collection
                {
                    //n.Attributes.Deserialize(reader);
                    DDAttributesCollectionSje.JsonDeserialize(n.Attributes, reader);
                }

                if ((reader.TokenType == JsonToken.StartArray) && (prevName == DDSchema.JSON_SERIALIZE_NODE) && (n != null)) // nodes collection
                {
                    while (reader.Read())
                    {
                        if (reader.TokenType == JsonToken.EndArray)
                        {
                            break;                                         // end list of nodes
                        }
                        if (reader.TokenType == JsonToken.StartObject)
                        {
                            n.Add(Deserialize(reader));                                            // end list of nodes
                        }
                    }
                }
                //  save current values
                prevTokenType = reader.TokenType;
                if (reader.TokenType == JsonToken.None)
                {
                    prevValueString = null;
                    prevName        = null;
                }
                else if (reader.TokenType == JsonToken.PropertyName)
                {
                    prevName = reader.Value.ToString();
                }
                else if (reader.TokenType == JsonToken.String)
                {
                    prevValueString = reader.Value.ToString();
                }
            }
            return(n);
        }