private JsonOptions(JsonSerializerDefaults defaults, JsonNamingPolicy namingPolicy = null)
        {
            SerializerSettings = new JsonSerializerOptions(defaults)
            {
                PropertyNamingPolicy = namingPolicy,
                Converters           =
                {
                    new JsonStringEnumConverter(namingPolicy),
                    new JsonStringEnumConverter()
                }
            };

            if (defaults == JsonSerializerDefaults.Web)
            {
                SerializerSettings.NumberHandling              = JsonNumberHandling.AllowReadingFromString;
                SerializerSettings.AllowTrailingCommas         = true;
                SerializerSettings.ReadCommentHandling         = JsonCommentHandling.Skip;
                SerializerSettings.PropertyNameCaseInsensitive = true;
                SerializerSettings.Encoder                  = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
                SerializerSettings.WriteIndented            = true;
                SerializerSettings.IgnoreNullValues         = true;
                SerializerSettings.DictionaryKeyPolicy      = namingPolicy;
                SerializerSettings.IgnoreReadOnlyFields     = true;
                SerializerSettings.IgnoreReadOnlyProperties = true;
                SerializerSettings.IncludeFields            = true;
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Constructs a new <see cref="JsonSerializerOptions"/> instance with a predefined set of options determined by the specified <see cref="JsonSerializerDefaults"/>.
 /// </summary>
 /// <param name="defaults"> The <see cref="JsonSerializerDefaults"/> to reason about.</param>
 public JsonSerializerOptions(JsonSerializerDefaults defaults) : this()
 {
     if (defaults == JsonSerializerDefaults.Web)
     {
         _propertyNameCaseInsensitive = true;
         _jsonPropertyNamingPolicy    = JsonNamingPolicy.CamelCase;
     }
     else if (defaults != JsonSerializerDefaults.General)
     {
         throw new ArgumentOutOfRangeException(nameof(defaults));
     }
 }