Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of the StreamingContext parameter in Json.NET Serialization Callbacks?

I'm trying to understand what were the StreamingContext parameter supposed to contain in Json.NET Serialization Callbacks, first I thought you would allow me access to the current json tree that is being read, but it doesn't seem to that, I tried may arrangements of JSON objects, but with none of them I could get anything from the StreamingContext parameter.

Here is an example that shows what I have being doing please correct me if I'm wrong:

using System;
using System.Runtime.Serialization;
using Newtonsoft.Json;

namespace Testes
{
    public class Program
    {
        [JsonObject(MemberSerialization.OptIn)]
        public class Person
        {
            [JsonProperty("id")]
            public int Id { get; set; }

            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("age")]
            public int Age { get; set; }

            [OnDeserialized]
            internal void OnDeserializedMethod(StreamingContext context)
            {
                Console.WriteLine(String.Format("OnDeserialized: {0}", context.Context));
            }

            [OnDeserializing]
            internal void OnDeserializingMethod(StreamingContext context)
            {
                Console.WriteLine(String.Format("OnDeserializing: {0}", context.Context));
            }
        }

        public static void Main(string[] args)
        {
            var lucy = JsonConvert.DeserializeObject<Person>("{ 'id': 1, 'name': 'Lucy', 'age': 22 }");

            Console.ReadKey();
        }
    }
}
like image 967
Zignd Avatar asked Oct 27 '14 21:10

Zignd


People also ask

What is StreamingContext C#?

StreamingContext(StreamingContextStates) Initializes a new instance of the StreamingContext class with a given context state. StreamingContext(StreamingContextStates, Object) Initializes a new instance of the StreamingContext class with a given context state, and some additional information.

What is the use of Jsonproperty attribute in C#?

JsonPropertyAttribute indicates that a property should be serialized when member serialization is set to opt-in. It includes non-public properties in serialization and deserialization. It can be used to customize type name, reference, null, and default value handling for the property value.

Which of the following attributes are required to ignore properties for JSON serializer?

To ignore individual properties, use the [JsonIgnore] attribute.

What is Jsonconvert DeserializeObject?

DeserializeObject<T>(String,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to a .


2 Answers

Good question. I've often wondered this myself, so you've inspired me to find out.

Searching through the Json.Net source code, it appears the StreamingContext is not really used much by the serializer at all, but is instead merely passed through from the serializer settings to other places that might need it. My guess is this was added to support the .NET ISerializable interface, whose contract requires implementors to provide a constructor that accepts a StreamingContext. Json.Net provides an empty StreamingContext by default, but allows you to set it explicitly in the settings if you need to. You can see this yourself with a small change to your Main method:

public static void Main(string[] args)
{
    JsonSerializerSettings settings = new JsonSerializerSettings
    {
        Context = new StreamingContext(StreamingContextStates.Other, "foo")
    };

    var json = @"{ ""id"": 1, ""name"": ""Lucy"", ""age"": 22 }";
    var lucy = JsonConvert.DeserializeObject<Person>(json, settings);

    Console.ReadKey();
}

Output:

OnDeserializing: foo
OnDeserialized: foo

In short, the StreamingContext parameter is not going to be very useful in most cases (since it is empty by default). It definitely does not provide access to the JSON tree being serialized or deserialized.

like image 177
Brian Rogers Avatar answered Oct 02 '22 09:10

Brian Rogers


StreamingContext is about serialization\deserialization environment, not particular target of it. Could be useful for passwords, formats and other custom s\d logic.

like image 1
Denys Avatar answered Oct 02 '22 09:10

Denys