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();
}
}
}
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.
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.
To ignore individual properties, use the [JsonIgnore] attribute.
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 .
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.
StreamingContext is about serialization\deserialization environment, not particular target of it. Could be useful for passwords, formats and other custom s\d logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With