예제 #1
0
        public static string SwifterToJson <T>(this T obj, string dateTimeFormat, JsonFormatterOptions options = JsonFormatterOptions.Default)
        {
            var format = new JsonFormatter(options);

            format.SetDateTimeFormat(dateTimeFormat.IsEmpty() ? DateTimeExt.DateTimeFormat : dateTimeFormat);
            return(format.Serialize(obj));
        }
예제 #2
0
 public static object DeserializeObject(string text, Type type, JsonFormatterOptions options = JsonFormatterOptions.Default)
 {
     fixed(char *chars = text)
     {
         return(ValueInterface.GetInterface(type).ReadValue(new JsonDeserializer(chars, 0, text.Length, options)));
     }
 }
예제 #3
0
 public static T DeserializeObject <T>(string text, JsonFormatterOptions options = JsonFormatterOptions.Default)
 {
     fixed(char *chars = text)
     {
         return(ValueInterface <T> .Content.ReadValue(new JsonDeserializer(chars, 0, text.Length, options)));
     }
 }
 public static T DeserializeObject <T>(string text, JsonFormatterOptions options)
 {
     fixed(char *chars = text)
     {
         return(DeserializeObject <T>(chars, text.Length, options));
     }
 }
 public static object DeserializeObject(string text, Type type, JsonFormatterOptions options)
 {
     fixed(char *chars = text)
     {
         return(DeserializeObject(chars, text.Length, type, options));
     }
 }
        public static object DeserializeObject(char *chars, int length, Type type, JsonFormatterOptions options)
        {
            if ((options & JsonFormatterOptions.MultiReferencingReference) != 0)
            {
                var jsonDeserializer = new JsonReferenceDeserializer(chars, length);

                var result = ValueInterface.GetInterface(type).Read(jsonDeserializer);

                if (jsonDeserializer.references.Count != 0)
                {
                    ReferenceInfo.ProcessReference(result, jsonDeserializer.references);

                    if (jsonDeserializer.updateBase)
                    {
                        result = RWHelper.GetContent <object>(jsonDeserializer.writer);

                        result = Convert.ChangeType(result, type);
                    }
                }

                return(result);
            }

            return(DeserializeObject(chars, length, type));
        }
예제 #7
0
        public static T DeserializeObject <T>(string text, JsonFormatterOptions options)
        {
            if ((options & JsonFormatterOptions.MultiReferencingReference) != 0)
            {
                fixed(char *chars = text)
                {
                    var jsonDeserializer = new JsonReferenceDeserializer(chars, 0, text.Length);

                    var result = ValueInterface <T> .Content.ReadValue(jsonDeserializer);

                    if (jsonDeserializer.references.Count != 0)
                    {
                        ReferenceInfo.ProcessReference(result, jsonDeserializer.references);

                        if (jsonDeserializer.updateBase)
                        {
                            result = RWHelper.GetContent <T>(jsonDeserializer.writer);
                        }
                    }

                    return(result);
                }
            }

            return(DeserializeObject <T>(text));
        }
        static BaseJsonSerializer CreateJsonSerializer(JsonFormatterOptions options)
        {
            if ((options & ReferenceOptions) != 0)
            {
                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    return(new JsonReferenceSerializer(options)
                    {
                        indentedChars = DefaultIndentedChars,
                        lineBreak = DefaultLineCharsBreak,
                        middleChars = DefaultMiddleChars
                    });
                }

                return(new JsonReferenceSerializer(options));
            }

            if ((options & ~JsonFormatterOptions.PriorCheckReferences) == JsonFormatterOptions.Default)
            {
                return(new JsonDefaultSerializer(DefaultMaxDepth));
            }

            if ((options & JsonFormatterOptions.Indented) != 0)
            {
                return(new JsonSerializer(options, DefaultMaxDepth)
                {
                    indentedChars = DefaultIndentedChars,
                    lineBreak = DefaultLineCharsBreak,
                    middleChars = DefaultMiddleChars
                });
            }

            return(new JsonSerializer(options, DefaultMaxDepth));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonSerializationInputFormatter"/> class.
 /// </summary>
 public JsonSerializationInputFormatter(JsonFormatterOptions formatterOptions)
 {
     SupportedEncodings.Add(Encoding.UTF8);
     SupportedEncodings.Add(Encoding.Unicode);
     SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
     SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/json"));
     FormatterOptions = formatterOptions;
 }
예제 #10
0
 /// <summary>
 /// 序列化对象到 JSON 字符串
 /// </summary>
 /// <param name="obj">要序列化的对象</param>
 /// <param name="sets">序列化设置</param>
 /// <returns></returns>
 public static string ToJson(object obj, JsonFormatterOptions sets = JsonFormatterOptions.Default)
 {
     if (obj == null)
     {
         return(null);
     }
     return(JsonFormatter.SerializeObject(obj, sets));
 }
        public static object DeserializeObject(TextReader textReader, Type type, JsonFormatterOptions options)
        {
            using (var hGCache = HGlobalCache <char> .OccupancyInstance())
            {
                var length = hGCache.Buffer(textReader);

                return(DeserializeObject(hGCache.GetPointer(), length, type, options));
            }
        }
예제 #12
0
 static T DeserializeObject <T>(char *chars, int length, JsonFormatterOptions options)
 {
     return((options & ModeOptions) switch
     {
         JsonFormatterOptions.MultiReferencingReference => DeserializeObject <T, JsonDeserializeModes.Reference>(chars, length),
         JsonFormatterOptions.DeflateDeserialize => DeserializeObject <T, JsonDeserializeModes.Deflate>(chars, length),
         JsonFormatterOptions.StandardDeserialize => DeserializeObject <T, JsonDeserializeModes.Standard>(chars, length),
         _ => DeserializeObject <T, JsonDeserializeModes.Verified>(chars, length),
     });
예제 #13
0
        public static string Serialize <T>(T o, JsonFormatterOptions options, string dateTimeFormat = null)
        {
            var jsonFormatter = new JsonFormatter(options);

            if (!string.IsNullOrWhiteSpace(dateTimeFormat))
            {
                jsonFormatter.SetDateTimeFormat(dateTimeFormat);
            }
            return(jsonFormatter.Serialize(o));
        }
예제 #14
0
        public static void SerializeObject <T>(T value, TextWriter textWriter, JsonFormatterOptions options)
        {
            if ((options & ReferenceOptions) != 0)
            {
                var jsonSerializer = new JsonReferenceSerializer(options);

                jsonSerializer.textWriter = textWriter;

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.indentedChars = DefaultIndentedChars;
                    jsonSerializer.lineBreak     = DefaultLineBreak;
                    jsonSerializer.middleChars   = DefaultMiddleChars;
                }

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);

                return;
            }

            if ((options & JsonFormatterOptions.PriorCheckReferences) != 0)
            {
                options ^= JsonFormatterOptions.PriorCheckReferences;
            }

            if (options == JsonFormatterOptions.Default)
            {
                var jsonSerializer = new JsonDefaultSerializer(DefaultMaxDepth);

                jsonSerializer.textWriter = textWriter;

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);
            }
            else
            {
                var jsonSerializer = new JsonSerializer(options, DefaultMaxDepth);

                jsonSerializer.textWriter = textWriter;

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.indentedChars = DefaultIndentedChars;
                    jsonSerializer.lineBreak     = DefaultLineBreak;
                    jsonSerializer.middleChars   = DefaultMiddleChars;
                }

                ValueInterface <T> .Content.WriteValue(jsonSerializer, value);

                jsonSerializer.WriteTo(textWriter);
            }
        }
예제 #15
0
 /// <summary>
 /// 反序列化 JSON 到对象
 /// </summary>
 /// <typeparam name="T">要序列化成哪种对象</typeparam>
 /// <param name="json">JSON 字符</param>
 /// <param name="sets">反序列化设置</param>
 /// <returns></returns>
 public static T ToObject <T>(string json, JsonFormatterOptions sets = JsonFormatterOptions.Default)
 {
     try
     {
         return(JsonFormatter.DeserializeObject <T>(json, sets));
     }
     catch (Exception e)
     {
         Yus.Diagnostics.YusDebug.ConsoleLog(e, nameof(ToObject));
     }
     return(default);
        public static string SerializeObject <T>(T value, JsonFormatterOptions options)
        {
            using (var jsonSerializer = CreateJsonSerializer(options))
            {
                ValueInterface <T> .WriteValue((IValueWriter)jsonSerializer, value);

                return(new string(
                           jsonSerializer.hGlobal.GetPointer(),
                           0,
                           jsonSerializer.StringLength));
            }
        }
예제 #17
0
        public static T DeserializeObject <T>(Stream stream, Encoding encoding, JsonFormatterOptions options)
        {
            var hGCache = CharsPool.Rent();

            hGCache.ReadFrom(stream, encoding);

            var value = DeserializeObject <T>(hGCache, options);

            CharsPool.Return(hGCache);

            return(value);
        }
        public static void SerializeObject <T>(T value, TextWriter textWriter, JsonFormatterOptions options)
        {
            using (var jsonSerializer = CreateJsonSerializer(options))
            {
                ValueInterface <T> .WriteValue((IValueWriter)jsonSerializer, value);

                VersionDifferences.WriteChars(
                    textWriter,
                    jsonSerializer.hGlobal.GetPointer(),
                    jsonSerializer.StringLength);
            }
        }
예제 #19
0
        public static object DeserializeObject(TextReader textReader, Type type, JsonFormatterOptions options)
        {
            var hGCache = CharsPool.Rent();

            hGCache.ReadFrom(textReader);

            var value = DeserializeObject(hGCache, type, options);

            CharsPool.Return(hGCache);

            return(value);
        }
예제 #20
0
        public static string SerializeObject <T>(T value, JsonFormatterOptions options)
        {
            var hGCache = CharsPool.Rent();

            SerializeObject(value, hGCache, options);

            var str = hGCache.ToStringEx();

            CharsPool.Return(hGCache);

            return(str);
        }
예제 #21
0
        public JsonReferenceSerializer(JsonFormatterOptions options)
        {
            if ((options & JsonFormatterOptions.PriorCheckReferences) != 0 &&
                (options & (JsonFormatterOptions.MultiReferencingNull | JsonFormatterOptions.MultiReferencingReference)) == 0)
            {
                options ^= JsonFormatterOptions.PriorCheckReferences;
            }

            this.options = options;

            references = new ReferenceCache <TargetPathInfo>();

            reference = new TargetPathInfo("#", null);
        }
예제 #22
0
        public static void SerializeObject <T>(T value, TextWriter textWriter, JsonFormatterOptions options)
        {
            var hGCache = CharsPool.Rent();

            if ((options & ReferenceOptions) != 0)
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.ReferenceMode>(options, hGCache, DefaultMaxDepth)
                {
                    References = new JsonReferenceWriter()
                };

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = DefaultIndentedChars;
                    jsonSerializer.LineBreakChars = DefaultLineCharsBreak;
                    jsonSerializer.MiddleChars    = DefaultMiddleChars;
                }

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
            else if ((options & ComplexOptions) != 0)
            {
                var jsonSerializer = new JsonSerializer <JsonSerializeModes.ComplexMode>(options, hGCache, DefaultMaxDepth);

                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = DefaultIndentedChars;
                    jsonSerializer.LineBreakChars = DefaultLineCharsBreak;
                    jsonSerializer.MiddleChars    = DefaultMiddleChars;
                }

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }
            else
            {
                var jsonSerializer = new JsonSerializer <DefaultSerializeMode>(options, hGCache, DefaultMaxDepth);

                ValueInterface <T> .WriteValue(jsonSerializer, value);

                jsonSerializer.Flush();
            }

            hGCache.WriteTo(textWriter);

            CharsPool.Return(hGCache);
        }
예제 #23
0
    /// <summary>
    /// 为 AspNet 添加来自 Swifter.Json 高性能的 Json 格式支持。
    /// </summary>
    /// <param name="services">Service 集合</param>
    /// <param name="jsonFormatterOptions">Json 格式器配置</param>
    /// <param name="configuration">配置回调</param>
    public static void ConfigureJsonFormatter(this IServiceCollection services, JsonFormatterOptions jsonFormatterOptions = JsonFormatterOptions.Default, Action <JsonFormatter> configuration = null)
    {
        void RemoveJsonFormatter <T>(FormatterCollection <T> formatters)
        {
            for (int i = formatters.Count - 1; i >= 0; --i)
            {
                if (formatters[i]?.GetType().FullName.IndexOf("Json", StringComparison.CurrentCultureIgnoreCase) + 1 != 0)
                {
                    formatters.RemoveAt(i);
                }
            }
        }

        var types = TypeHelper.GetTypes("Microsoft.AspNetCore.Mvc.JsonResult");

        var jsonFormatter = new JsonFormatter(jsonFormatterOptions);

        configuration?.Invoke(jsonFormatter);

        foreach (var item in types)
        {
            if (typeof(IActionResult).IsAssignableFrom(item))
            {
                try
                {
                    var executorType = typeof(AspNetJsonExecutor <>).MakeGenericType(item);
                    var serviceType  = typeof(IActionResultExecutor <>).MakeGenericType(item);

                    var executor = Activator.CreateInstance(executorType, jsonFormatter);

                    services.AddSingleton(serviceType, executor);
                }
                catch (Exception)
                {
                }
            }
        }

        services.Configure <MvcOptions>(mvcOptions =>
        {
            RemoveJsonFormatter(mvcOptions.InputFormatters);
            RemoveJsonFormatter(mvcOptions.OutputFormatters);

            var formatter = new AspNetJsonFormatter(jsonFormatter);

            mvcOptions.InputFormatters.Add(formatter);
            mvcOptions.OutputFormatters.Add(formatter);
        });
    }
예제 #24
0
        public static void SerializeObject <T>(T value, TextWriter textWriter, JsonFormatterOptions options)
        {
            var serializer = new JsonSerializer(options, DefaultMaxDepth);

            if ((options & JsonFormatterOptions.Indented) != 0)
            {
                serializer.indentedChars = DefaultIndentedChars;
                serializer.lineBreak     = DefaultLineBreak;
                serializer.middleChars   = DefaultMiddleChars;
            }

            ValueInterface <T> .Content.WriteValue(serializer, value);

            serializer.WriteTo(textWriter);
        }
예제 #25
0
        public static object DeserializeObject(char *chars, int length, Type type, JsonFormatterOptions options)
        {
            switch (options & ModeOptions)
            {
            case JsonFormatterOptions.MultiReferencingReference:
                return(ValueInterface.GetInterface(type).Read(new JsonDeserializer <JsonDeserializeModes.Reference>(chars, length)));

            case JsonFormatterOptions.DeflateDeserialize:
                return(ValueInterface.GetInterface(type).Read(new JsonDeserializer <JsonDeserializeModes.Deflate>(chars, length)));

            case JsonFormatterOptions.StandardDeserialize:
                return(ValueInterface.GetInterface(type).Read(new JsonDeserializer <JsonDeserializeModes.Standard>(chars, length)));

            default:
                return(ValueInterface.GetInterface(type).Read(new JsonDeserializer <DefaultDeserializeMode>(chars, length)));
            }
        }
예제 #26
0
        public JsonSerializer(JsonFormatterOptions options, int maxDepth)
        {
            this.options = options;

            this.maxDepth = maxDepth;

            offset = 0;

            hGlobal = HGlobalChars.ThreadInstance;

            Expand(255);

            if ((options & (JsonFormatterOptions.MultiReferencingNull | JsonFormatterOptions.MultiReferencingReference)) != 0)
            {
                objectIds = new IdCache <int>();
            }
        }
예제 #27
0
        public JsonDeserializer(char *chars, int index, int length, JsonFormatterOptions options)
        {
            if (index >= length)
            {
                throw new ArgumentException("Json text cannot be empty.");
            }

            if ((options & JsonFormatterOptions.MultiReferencingReference) != 0)
            {
                objects = new List <IDataWriter>();
            }

            this.options = options;
            this.chars   = chars;
            this.index   = index;
            this.length  = length;
        }
예제 #28
0
        static void SerializeObject <T, TMode>(T value, HGlobalCache <char> hGCache, JsonFormatterOptions options) where TMode : struct
        {
            var jsonSerializer = new JsonSerializer <TMode>(hGCache, DefaultMaxDepth, options);

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode))
            {
                jsonSerializer.References = new JsonReferenceWriter();
            }

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode) || typeof(TMode) == typeof(JsonSerializeModes.ComplexMode))
            {
                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = DefaultIndentedChars;
                    jsonSerializer.LineBreakChars = DefaultLineCharsBreak;
                    jsonSerializer.MiddleChars    = DefaultMiddleChars;
                }
            }

            ValueInterface <T> .WriteValue(jsonSerializer, value);

            jsonSerializer.Flush();
        }
예제 #29
0
        void Serialize <T, TMode>(T value, HGlobalCache <char> hGCache, JsonFormatterOptions options)
        {
            var jsonSerializer = new JsonSerializer <JsonSerializeModes.ReferenceMode>(this, hGCache, MaxDepth, options);

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode))
            {
                jsonSerializer.References = new JsonReferenceWriter();
            }

            if (typeof(TMode) == typeof(JsonSerializeModes.ReferenceMode) || typeof(TMode) == typeof(JsonSerializeModes.ComplexMode))
            {
                if ((options & JsonFormatterOptions.Indented) != 0)
                {
                    jsonSerializer.IndentedChars  = IndentedChars;
                    jsonSerializer.LineBreakChars = LineBreakChars;
                    jsonSerializer.MiddleChars    = MiddleChars;
                }
            }

            ValueInterface <T> .WriteValue(jsonSerializer, value);


            jsonSerializer.Flush();
        }
 /// <summary>
 /// 为 AspNet 添加来自 Swifter.Json 高性能的 Json 格式支持。
 /// </summary>
 /// <param name="services">Service 集合</param>
 /// <param name="jsonFormatterOptions">Json 格式器配置</param>
 /// <param name="configuration">配置回调</param>
 public static void ConfigureJsonFormatter(this IServiceCollection services, JsonFormatterOptions jsonFormatterOptions = JsonFormatterOptions.Default, Action <JsonFormatter> configuration = null)
 {