Example #1
0
 public ValueTask <T> DeserializeAsync <T>(string text, TextSerializationOptions options = null, CancellationToken cancellationToken = default)
 {
     using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
     {
         return(JsonSerializer.DeserializeAsync <T>(stream, GetOptions(options), cancellationToken));
     }
 }
Example #2
0
            public async Task <string> SerializeAsync(object graph, TextSerializationOptions options = null, CancellationToken cancellationToken = default)
            {
                using (var stream = new MemoryStream())
                {
                    await JsonSerializer.SerializeAsync(stream, graph, GetOptions(options), cancellationToken);

                    using (var reader = new StreamReader(stream, Encoding.UTF8, false))
                    {
                        return(await reader.ReadToEndAsync());
                    }
                }
            }
Example #3
0
        protected override object OnExecute(CommandContext context)
        {
            var graph = context.Parameter;

            if (graph == null)
            {
                return(null);
            }

            //如果输入参数是文本或流或文本读取器,则反序列化它并返回
            if (graph is string raw)
            {
                return(Serializer.Json.Deserialize <Dictionary <string, object> >(raw));
            }
            if (graph is System.Text.StringBuilder text)
            {
                return(Serializer.Json.Deserialize <Dictionary <string, object> >(text.ToString()));
            }
            if (graph is Stream stream)
            {
                return(Serializer.Json.Deserialize <Dictionary <string, object> >(stream));
            }

            var options = new TextSerializationOptions()
            {
                MaximumDepth     = context.Expression.Options.GetValue <int>(KEY_DEPTH_OPTION),
                Typed            = context.Expression.Options.GetValue <bool>(KEY_TYPED_OPTION),
                Indented         = context.Expression.Options.GetValue <bool>(KEY_INDENTED_OPTION),
                NamingConvention = context.Expression.Options.GetValue <SerializationNamingConvention>(KEY_CASING_OPTION),
                IncludeFields    = true,
            };

            var json = Serializer.Json.Serialize(graph, options);

            if (json != null)
            {
                context.Output.WriteLine(json);
            }

            return(json);
        }
Example #4
0
            private static JsonSerializerOptions GetOptions(TextSerializationOptions options)
            {
                if (options == null)
                {
                    return(DefaultOptions);
                }

                JsonNamingPolicy naming = null;

                switch (options.NamingConvention)
                {
                case SerializationNamingConvention.Camel:
                    naming = JsonNamingConvention.Camel;
                    break;

                case SerializationNamingConvention.Pascal:
                    naming = JsonNamingConvention.Pascal;
                    break;
                }

                return(new JsonSerializerOptions()
                {
                    Encoder = DefaultOptions.Encoder,
                    PropertyNameCaseInsensitive = true,
                    MaxDepth = options.MaximumDepth,
                    WriteIndented = options.Indented,
                    IgnoreNullValues = options.IgnoreNull,
                    IgnoreReadOnlyProperties = false,
                    PropertyNamingPolicy = naming,
                    DictionaryKeyPolicy = naming,
                    Converters =
                    {
                        new JsonTimeSpanConverter(),
                        new JsonStringEnumConverter(naming),
                        new ModelConverterFactory(),
                        new RangeConverterFactory(),
                        new NullableConverterFactory(),
                    },
                });
            }
Example #5
0
 public string Serialize(object graph, TextSerializationOptions options = null)
 {
     return(JsonSerializer.Serialize(graph, GetOptions(options)));
 }
Example #6
0
 public ValueTask <object> DeserializeAsync(string text, TextSerializationOptions options = null, CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
Example #7
0
 public T Deserialize <T>(string text, TextSerializationOptions options = null)
 {
     return(JsonSerializer.Deserialize <T>(text, GetOptions(options)));
 }
Example #8
0
 public object Deserialize(string text, Type type, TextSerializationOptions options = null)
 {
     return(JsonSerializer.Deserialize(text, type, GetOptions(options)));
 }
Example #9
0
 public object Deserialize(string text, TextSerializationOptions options = null)
 {
     throw new NotImplementedException();
 }