Пример #1
0
        static void ObjectExpectKey_Accept(ParserContext context, ParserContext innerContext)
        {
            JsonLiteObject instance = (JsonLiteObject)context.Result;

            context.Text  = innerContext.Text;
            context.State = JsonLiteParserState.ObjectExpectColon;
        }
Пример #2
0
        static void ObjectExpectValue_Accept(ParserContext context, ParserContext innerContext)
        {
            JsonLiteObject instance = (JsonLiteObject)context.Result;

            if (innerContext.State == JsonLiteParserState.ArrayExpectValue || innerContext.State == JsonLiteParserState.ObjectExpectValue ||
                innerContext.State == JsonLiteParserState.ArrayExpectComma || innerContext.State == JsonLiteParserState.ObjectExpectComma ||
                innerContext.State == JsonLiteParserState.ObjectExpectKey)
            {
                instance.Properties[context.Text.ToString()] = innerContext.Result;
                context.State = JsonLiteParserState.ObjectExpectComma;
            }
            else if (innerContext.State == JsonLiteParserState.ExpectQuotedValue || innerContext.State == JsonLiteParserState.ExpectValue)
            {
                instance.Properties[context.Text.ToString()] = new JsonLiteValue()
                {
                    Value = innerContext.Text.ToString()
                };
                context.State = JsonLiteParserState.ObjectExpectComma;
            }
            //else if (innerContext.State == JsonLiteParserState.ExpectValue) {
            //    instance.Properties[context.Text.ToString()] = new JsonLiteValue() { Value = innerContext.Text.ToString() };
            //    //if (innerContext.Char == ',') {
            //    //    context.State = JsonLiteParserState.ObjectExpectKey;
            //    //    context.Text = new StringBuilder();
            //    //}
            //    //else
            //        context.State = JsonLiteParserState.ObjectExpectComma;
            //}
            else
            {
                context.State = JsonLiteParserState.ParseFailed;
            }
        }
Пример #3
0
        static void ApplyProperties(JsonLiteObjectBase instance, object result)
        {
            Type          type  = result.GetType();
            JsonLiteArray array = instance as JsonLiteArray;

            if (array != null)
            {
                ApplyPropertiesCore(array, result);
                return;
            }
            JsonLiteObject obj = instance as JsonLiteObject;

            if (obj != null)
            {
                ApplyPropertiesCore(obj, result);
                return;
            }
        }
Пример #4
0
        static void ApplyPropertiesCore(JsonLiteObject obj, object result)
        {
            if (obj == null)
            {
                return;
            }

            if (obj.Properties == null || obj.Properties.Count <= 0)
            {
                return;
            }

            foreach (string key in obj.Properties.Keys)
            {
                PropertyInfo pi = result.GetType().GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (pi != null)
                {
                    ApplyProperty(result, pi, obj.Properties[key]);
                }
            }
        }
Пример #5
0
        static object ConvertToObject(JsonLiteObjectBase value, Type propertyType)
        {
            JsonLiteObject jsonValue = value as JsonLiteObject;

            if (jsonValue == null)
            {
                JsonLiteValue val = value as JsonLiteValue;
                if (val != null && val.Value == "null")
                {
                    return(null);
                }

                throw new JsonLiteDeserializationException();
            }

            object instance = Activator.CreateInstance(propertyType);

            if (instance != null)
            {
                ApplyProperties(value, instance);
            }
            return(instance);
        }