Exemplo n.º 1
0
        public static T DeserializeObject <T>(string value)
        {
            Type t = typeof(T);

            if (typeof(JsonSerializible).IsAssignableFrom(t))
            {
                JsonValue jsonValue = new JsonReader(value).Read();
                return(t.CreateInstance <T>(jsonValue));
            }
            else if (typeof(IJsonSerializible).IsAssignableFrom(t))
            {
                JsonValue         jsonValue = new JsonReader(value).Read();
                IJsonSerializible instance  = (IJsonSerializible)t.CreateInstance <T>(jsonValue);
                instance.FromJson(jsonValue);
                return((T)instance);
            }
            else if (JsonWellKnownTypes.TryGetValue(t, out var f))
            {
                JsonValue jsonValue = new JsonReader(value).Read();
                var       instance  = t.CreateInstance();
                f.ReadDelegate(jsonValue, instance);
                return((T)instance);
            }
#if NETCOREAPP
            else
            {
                return(System.Text.Json.JsonSerializer.Deserialize <T>(value, _serializerOptions));
            }
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 2
0
        public static string SerializeObject(object value, Formatting formatting = Formatting.None)
        {
            if (value is IJsonSerializible js)
            {
                return(js.ToString(formatting));
            }
            else if (JsonWellKnownTypes.TryGetValue(value.GetType(), out var f))
            {
                JsonWriter writer = new JsonWriter(formatting);
                f.WriteDelegate(writer, value);
                return(writer.ToString());
            }
#if NETCOREAPP
            else
            {
                return(System.Text.Json.JsonSerializer.Serialize(value, _serializerOptions));
            }
#else
            throw new NotImplementedException();
#endif
        }
Exemplo n.º 3
0
        public JsonWriter WriteValue(JsonValue v)
        {
            switch (v.JsonType)
            {
            case JsonType.Object:
                ((JsonObject)v).WriteRaw(this);
                _commaExpected = true;
                break;

            case JsonType.Array:
                ((JsonArray)v).WriteRaw(this);
                _commaExpected = true;
                break;

            case JsonType.Null:
                WriteNull();
                break;

            case JsonType.String:
                WriteValue((string)v.Value);
                break;

            case JsonType.Float:
                WriteValue(Convert.ToDouble(v.Value));
                break;

            case JsonType.Integer:
                WriteValue(Convert.ToInt64(v.Value));
                break;

            case JsonType.Date:
                WriteValue((DateTime)v.Value);
                break;

            case JsonType.Boolean:
                WriteValue((bool)v.Value);
                break;

            case JsonType.Guid:
                WriteValue((Guid)v.Value);
                break;

            case JsonType.Uri:
                WriteValue((Uri)v.Value);
                break;

            case JsonType.Bytes:
                WriteValue((byte[])v.Value);
                break;

            case JsonType.TimeSpan:
                WriteValue((TimeSpan)v.Value);
                break;

            default:
                if (v.Value is JsonSerializible js)
                {
                    js.ToJson(this);
                }
                else if (JsonWellKnownTypes.TryGetValue(v.Value.GetType(), out var f))
                {
                    f.WriteDelegate(this, v.Value);
                }
                else
                {
                    //see for enum
                    WriteValue(v.Value.ToString());
                }
                break;
            }
            return(this);
        }