Exemple #1
0
        /// <summary>
        /// Filters data from the specified object.
        /// </summary>
        /// <typeparam name="T">The type of the object.</typeparam>
        /// <param name="value">The object instance.</param>
        /// <param name="settings">The settings used when reading/writing JSON.</param>
        /// <returns>A new object with the specified data filtered.</returns>
        /// <remarks>This method converts the object to JSON, filtering out data as
        /// appropriate, and then converts the filtered JSON back into an object.</remarks>
        public T FilterObject <T>(T value, JsonSettings settings)
        {
            if (value == null)
            {
                return(default(T));
            }

            var tokenWriter = new JTokenWriter();

            JsonUtility.ToJsonWriter(value, settings, CreateFilteredJsonWriter(tokenWriter));
            return(JsonUtility.FromJToken <T>(tokenWriter.Token, settings));
        }
Exemple #2
0
        /// <summary>
        /// Creates default serialization settings.
        /// </summary>
        /// <returns>The serialization settings used by ToJson.</returns>
        public static JsonSerializerSettings CreateDefaultJsonSerializerSettings(JsonSettings settings)
        {
            JsonSerializerSettings serializerSettings =
                new JsonSerializerSettings
            {
                ContractResolver         = s_defaultContractResolver,
                DateParseHandling        = DateParseHandling.None,
                NullValueHandling        = settings?.IncludesNullValues ?? false ? NullValueHandling.Include : NullValueHandling.Ignore,
                MissingMemberHandling    = settings?.RejectsExtraProperties ?? false ? MissingMemberHandling.Error : MissingMemberHandling.Ignore,
                MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
                CheckAdditionalContent   = true,
            };

            if (settings?.Converters != null)
            {
                serializerSettings.Converters.AddRange(settings.Converters);
            }

            serializerSettings.Converters.AddRange(s_defaultConverters);

            return(serializerSettings);
        }
Exemple #3
0
        /// <summary>
        /// Converts the object to a JSON writer.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="jsonWriter">The JSON writer to write JSON to.</param>
        public static void ToJsonWriter(object value, JsonSettings settings, JsonWriter jsonWriter)
        {
            JsonSerializer serializer = JsonSerializer.Create(CreateDefaultJsonSerializerSettings(settings));

            serializer.Serialize(jsonWriter, value);
        }
Exemple #4
0
 /// <summary>
 /// Gets the JSON formatting specified by the settings.
 /// </summary>
 /// <param name="settings">The settings.</param>
 /// <returns>The JSON formatting.</returns>
 public static Formatting GetJsonFormatting(JsonSettings settings) => settings != null && settings.IsIndented ? Formatting.Indented : Formatting.None;
Exemple #5
0
 /// <summary>
 /// Creates an object from a JToken.
 /// </summary>
 /// <typeparam name="T">The type of object to create.</typeparam>
 /// <param name="json">The JToken.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 public static T FromJToken <T>(JToken json, JsonSettings settings) => (T)FromJToken(json, typeof(T), settings);
Exemple #6
0
 /// <summary>
 /// Creates an object from compressed JSON.
 /// </summary>
 /// <param name="json">The compressed JSON.</param>
 /// <param name="type">The type.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 /// <exception cref="JsonReaderException">The text is not valid JSON.</exception>
 /// <exception cref="JsonSerializationException">The JSON cannot be deserialized into the specified type.</exception>
 public static object FromCompressedJson(byte[] json, Type type, JsonSettings settings)
 {
     using (MemoryStream stream = new MemoryStream(json, false))
         using (TextReader textReader = StringUtility.CreateDecompressingTextReader(stream, Ownership.None))
             return(FromJsonTextReader(textReader, type, settings));
 }
Exemple #7
0
 /// <summary>
 /// Creates an object from compressed JSON.
 /// </summary>
 /// <typeparam name="T">The type of object to create.</typeparam>
 /// <param name="json">The compressed JSON.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 /// <exception cref="JsonReaderException">The text is not valid JSON.</exception>
 /// <exception cref="JsonSerializationException">The JSON cannot be deserialized into the specified type.</exception>
 public static T FromCompressedJson <T>(byte[] json, JsonSettings settings) => (T)FromCompressedJson(json, typeof(T), settings);
Exemple #8
0
 /// <summary>
 /// Creates an object from JSON.
 /// </summary>
 /// <param name="textReader">The JSON.</param>
 /// <param name="type">The type.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 /// <exception cref="JsonReaderException">The text is not valid JSON.</exception>
 /// <exception cref="JsonSerializationException">The JSON cannot be deserialized into the specified type.</exception>
 public static object FromJsonTextReader(TextReader textReader, Type type, JsonSettings settings)
 {
     using (JsonReader reader = new JsonTextReader(textReader))
         return(Deserialize(settings, reader, type));
 }
Exemple #9
0
 /// <summary>
 /// Creates an object from JSON.
 /// </summary>
 /// <param name="textReader">The JSON.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 /// <exception cref="JsonReaderException">The text is not valid JSON.</exception>
 /// <exception cref="JsonSerializationException">The JSON cannot be deserialized into the specified type.</exception>
 public static T FromJsonTextReader <T>(TextReader textReader, JsonSettings settings) => (T)FromJsonTextReader(textReader, typeof(T), settings);
Exemple #10
0
 /// <summary>
 /// Creates an object from JSON.
 /// </summary>
 /// <param name="json">The JSON.</param>
 /// <param name="type">The type.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 /// <exception cref="JsonReaderException">The text is not valid JSON.</exception>
 /// <exception cref="JsonSerializationException">The JSON cannot be deserialized into the specified type.</exception>
 public static object FromJson(string json, Type type, JsonSettings settings)
 {
     using (StringReader stringReader = new StringReader(json))
         return(FromJsonTextReader(stringReader, type, settings));
 }
Exemple #11
0
 /// <summary>
 /// Creates an object from JSON.
 /// </summary>
 /// <typeparam name="T">The type of object to create.</typeparam>
 /// <param name="json">The JSON.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The object.</returns>
 /// <exception cref="JsonReaderException">The text is not valid JSON.</exception>
 /// <exception cref="JsonSerializationException">The JSON cannot be deserialized into the specified type.</exception>
 public static T FromJson <T>(string json, JsonSettings settings) => (T)FromJson(json, typeof(T), settings);