Exemplo n.º 1
0
        private void SerializeJsonDictionaryObject(Stream stream, object value, bool omitDefaultValue)
        {
            stream.WriteByte(JsonEncoder.Left_Brace);

            var firstElement = true;

            foreach (var jsonProperty in JsonProperties.Values.ToArray())
            {
                var propertyValue = jsonProperty.PropertyInfo.GetValue(value, null);
                if (omitDefaultValue && jsonProperty.IsDefaultValue(propertyValue))
                {
                    continue;
                }

                if (firstElement)
                {
                    firstElement = false;
                }
                else
                {
                    stream.WriteByte(JsonEncoder.Comma);
                }

                var buf = JsonEncoder.GetElementName(jsonProperty.Key);
                stream.Write(buf, 0, buf.Length);

                SerializeRegularValue(stream, propertyValue, jsonProperty.ObjectType, omitDefaultValue);
            }

            stream.WriteByte(JsonEncoder.Right_Brace);
        }
Exemplo n.º 2
0
 private object DeserializeJsonPlainValueObject(JsonValueObject plainValueObject, Type targetType)
 {
     if (JsonEncoder.IsNullValue(plainValueObject))
     {
         return(null);
     }
     else
     {
         return(plainValueObject.ToString().ConvertTo(targetType));
     }
 }
Exemplo n.º 3
0
        private bool Parse(IStream stream)
        {
            var b = stream.SeekBytesUntilVisiableChar();

            if (b == JsonEncoder.Right_Brace)
            {
                return(true);
            }
            else if (b != JsonEncoder.Double_Quotes)
            {
                throw new Errors.JsonParseFailedException(stream.Position, "dictionary name is invalid.");
            }

            var buf = stream.ReadBytesUntil(JsonEncoder.Double_Quotes);

            if (buf == null)
            {
                buf = new byte[0];
            }

            var elementName = JsonEncoder.GetString(buf);

            if (!elementName.HasValue())
            {
                throw new Errors.JsonParseFailedException(stream.Position, "dictionary element name is empty.");
            }

            stream.SeekBytesUntilEqual(JsonEncoder.Colon);

            var args = new JsonObjectParseArgs()
            {
                ExternalObject = this,
                Stream         = stream,
            };

            JsonObjectParser.Parse(args);

            if (args.InternalObject != null)
            {
                Elements = Elements.Append(new JsonDictionaryElement()
                {
                    Key   = elementName,
                    Value = args.InternalObject,
                });
            }

            return(args.Handled);
        }
Exemplo n.º 4
0
 public override string ToString()
 {
     if (Buffer != null)
     {
         if (EncompassedByQuote)
         {
             return(JsonEncoder.GetString(Buffer));
         }
         else
         {
             return(JsonEncoder.GetString(Buffer).Trim());
         }
     }
     else
     {
         return(string.Empty);
     }
 }
Exemplo n.º 5
0
 private void SerializeRegularValue(Stream stream, object value, JsonObjectType objectType, bool omitDefaultValue)
 {
     if (objectType == JsonObjectType.Value)
     {
         var buf = JsonEncoder.GetPlainValue(value);
         stream.Write(buf, 0, buf.Length);
     }
     else if (value == null)
     {
         var buf = JsonEncoder.GetNullValue();
         stream.Write(buf, 0, buf.Length);
     }
     else if (objectType == JsonObjectType.Runtime)
     {
         SerializeRegularValue(stream, value, JsonUtility.GetJsonObjectType(value.GetType()), omitDefaultValue);
     }
     else
     {
         GetSerializer(value.GetType()).Serialize(value, stream, omitDefaultValue);
     }
 }
Exemplo n.º 6
0
        private byte[] GetUnescapeByteValues(IStream stream)
        {
            var buffer = new byte[0];

            var buf = new byte[0];

            while ((buf = stream.ReadBytesUntil(new byte[] { JsonEncoder.Double_Quotes, JsonEncoder.Backslash })) != null)
            {
                if (buf[buf.Length - 1] == JsonEncoder.Double_Quotes)
                {
                    return(Concat(buffer, 0, buffer.Length, buf, 0, buf.Length - 1));
                }

                if (buf[buf.Length - 1] == JsonEncoder.Backslash)
                {
                    var b = stream.ReadByte();
                    if (b == -1)
                    {
                        return(null);
                    }

                    if (b == JsonEncoder.U)
                    {
                        var escape = JsonEncoder.DoUnescape(stream.ReadBytes(4));
                        buf = Concat(buf, 0, buf.Length - 1, escape, 0, escape.Length);
                    }
                    else
                    {
                        buf[buf.Length - 1] = JsonEncoder.DoUnescape(b);
                    }

                    buffer = Concat(buffer, 0, buffer.Length, buf, 0, buf.Length);
                }
            }

            return(buffer);
        }
Exemplo n.º 7
0
        private object DeserializeJsonDictionaryObject(JsonDictionaryObject dictionaryObject)
        {
            var instance = Activator.CreateInstance(Type);

            foreach (var element in dictionaryObject.Elements)
            {
                JsonProperty jsonProperty;
                JsonProperties.TryGetValue(element.Key, out jsonProperty);
                if (jsonProperty == null)
                {
                    continue;
                }

                if (jsonProperty.IsJsonObject)
                {
                    jsonProperty.PropertyInfo.SetValue(instance, element.Value, null);
                    continue;
                }

                if (jsonProperty.ObjectType == JsonObjectType.Runtime)
                {
                    continue;
                }

                if (jsonProperty.ObjectType == JsonObjectType.Dictionary)
                {
                    if (element.Value is JsonDictionaryObject)
                    {
                        var value = GetSerializer(jsonProperty.PropertyInfo.PropertyType).InternalDeserialize(element.Value);
                        jsonProperty.PropertyInfo.SetValue(instance, value, null);
                    }
                    else if (element.Value is JsonValueObject && JsonEncoder.IsNullValue(element.Value))
                    {
                        jsonProperty.PropertyInfo.SetValue(instance, null, null);
                    }
                    else
                    {
                        throw new Errors.JsonSerializeFailedException(element.Key, ".net runtime type does not match json type.");
                    }
                }
                else if (jsonProperty.ObjectType == JsonObjectType.Collection)
                {
                    if (element.Value is JsonCollectionObject)
                    {
                        var collectionObject = element.Value as JsonCollectionObject;
                        jsonProperty.PropertyInfo.SetValue(instance,
                                                           DeserializeJsonCollectionObject(collectionObject, jsonProperty.PropertyInfo.PropertyType), null);
                    }
                    else if (element.Value is JsonValueObject && JsonEncoder.IsNullValue(element.Value))
                    {
                        jsonProperty.PropertyInfo.SetValue(instance, null, null);
                    }
                    else
                    {
                        throw new Errors.JsonSerializeFailedException(element.Key, ".net runtime type does not match json type.");
                    }
                }
                else if (jsonProperty.ObjectType == JsonObjectType.Value && element.Value is JsonValueObject)
                {
                    var value = DeserializeJsonPlainValueObject(element.Value as JsonValueObject, jsonProperty.PropertyInfo.PropertyType);
                    jsonProperty.PropertyInfo.SetValue(instance, value, null);
                }
                else
                {
                    throw new Errors.JsonSerializeFailedException(element.Key, ".net runtime type does not match json type.");
                }
            }

            return(instance);
        }