Exemplo n.º 1
0
Arquivo: Json.cs Projeto: i-e-b/ADSD
        /// <summary>
        /// Read a weakly-typed dictionary tree into a strong type. If the keys do not match exactly,
        /// all matching field/properties will be filled.
        /// If *no* keys match the target type, this will return `null`
        /// </summary>
        private object ParseDictionary(IDictionary <string, object> jsonValues, IDictionary <string, object> globaltypes, Type type, object input)
        {
            jsonParameters = jsonParameters ?? DefaultParameters;

            object tn;

            if (jsonValues.TryGetValue("$types", out tn))
            {
                var dic = ((Dictionary <string, object>)tn);
                foreach (var kvp in dic)
                {
                    globaltypes.Add((string)kvp.Value, kvp.Key);
                }

                usingGlobals = true;
            }

            var found = jsonValues.TryGetValue("$type", out tn);

            if (found == false && type == typeof(object))
            {
                return(CreateDataset(jsonValues, globaltypes));
            }
            if (found)
            {
                if (usingGlobals)
                {
                    object tname;
                    if (globaltypes.TryGetValue((string)tn, out tname))
                    {
                        tn = tname;
                    }
                }
                if (type == null || !type.IsInterface)
                {
                    type = GetTypeFromCache((string)tn);
                }
            }

            var targetObject = input ?? FastCreateInstance(type);

            if (targetObject == null)
            {
                return(jsonValues);                      // can't work out what object to fill, send back the raw values
            }
            var targetType = targetObject.GetType();

            var props = GetProperties(targetType, targetType.Name);

            if (!IsDictionary(targetType) && NoPropertiesMatch(props.Keys, jsonValues.Keys))
            {
                return(null);
            }
            foreach (var key in jsonValues.Keys)
            {
                MapJsonValueToObject(key, targetObject, jsonValues, globaltypes, props);
            }

            return(targetObject);
        }
Exemplo n.º 2
0
Arquivo: Json.cs Projeto: i-e-b/ADSD
 internal void ToJsonStream(object obj, Stream target, JsonParameters param, Encoding encoding)
 {
     jsonParameters = param.Clone();
     if (jsonParameters.EnableAnonymousTypes)
     {
         jsonParameters.UseExtensions = false; jsonParameters.UsingGlobalTypes = false;
     }
     new JsonSerializer(jsonParameters).ConvertToJson(obj, target, encoding);
 }
Exemplo n.º 3
0
Arquivo: Json.cs Projeto: i-e-b/ADSD
 internal string ToJson(object obj, JsonParameters param)
 {
     jsonParameters = param.Clone();
     if (jsonParameters.EnableAnonymousTypes)
     {
         jsonParameters.UseExtensions = false; jsonParameters.UsingGlobalTypes = false;
     }
     return(new JsonSerializer(jsonParameters).ConvertToJson(obj));
 }
Exemplo n.º 4
0
Arquivo: Json.cs Projeto: i-e-b/ADSD
        /// <summary>
        /// Create a new object by type, using input json data
        /// </summary>
        /// <param name="json">Either a stream of utf-8 data or an in-memory `string`</param>
        /// <param name="type">Target return type</param>
        /// <param name="encoding">String encoding to use, if reading from a stream</param>
        internal object ToObject(object json, Type type, Encoding encoding)
        {
            jsonParameters = jsonParameters ?? DefaultParameters;
            var globalTypes = new Dictionary <string, object>();

            var parser = ParserFromStreamOrStringOrBytes(json, encoding);

            var decodedObject = parser.Decode();

            return(StrengthenType(type, decodedObject, globalTypes));
        }
Exemplo n.º 5
0
Arquivo: Json.cs Projeto: i-e-b/ADSD
 private JsonTool()
 {
     DefaultParameters = DefaultParameters ?? new JsonParameters();
     jsonParameters    = DefaultParameters;
 }
Exemplo n.º 6
0
 public JsonSerializer(JsonParameters param)
 {
     jsonParameters = param;
 }