Exemplo n.º 1
0
    public static object DeserializeObject(JsonObject jsonObj, Type type)
    {
        if (jsonObj == null || type == null)
        {
            return(null);
        }
        if (type == typeof(JsonObject))
        {
            return(jsonObj);
        }

        object       entity = Activator.CreateInstance(type);
        JsonContract jcb    = JsonContract.GetJsonContractByType(type);

        if (type.IsValueType)
        {
            foreach (var property in jcb.Properties)
            {
                JsonValue jsonValue;
                if (!jsonObj.TryGetValue(property.Name, out jsonValue))
                {
                    continue;
                }

                JsonUtils.SetProperty(entity, property, jsonValue);
            }
            return(entity);
        }
        else
        {
            foreach (var property in jcb.Properties)
            {
                JsonValue jsonValue;
                if (!jsonObj.TryGetValue(property.Name, out jsonValue))
                {
                    continue;
                }

                JsonUtils.SetProperty(entity, property, jsonValue);
            }
            return(entity);
        }
    }
Exemplo n.º 2
0
    public static void DeserializeObject(object entity, JsonObject jsonObj)
    {
        if (jsonObj == null)
        {
            return;
        }
        Type         type = entity.GetType();
        JsonContract jcb  = JsonContract.GetJsonContractByType(type);

        foreach (var property in jcb.Properties)
        {
            JsonValue jsonValue;
            if (!jsonObj.TryGetValue(property.Name, out jsonValue))
            {
                continue;
            }

            JsonUtils.SetProperty(entity, property, jsonValue);
        }
    }