2

Using Json.NET are Serialization Callbacks supported when the OnDeserializedAttribute is placed on a base class method? For example using this object graph:

[DataContract]
public class StubData:StubBase {}

[DataContract]
public class StubBase {
    public string Id { get; set; }

    [OnDeserialized]
    public void OnDeserialized(StreamingContext context) {
        Id = "1";
    }
}

var stubData = JsonConvert.DeserializeObject<StubData>(@"{""anyData"":""Foo""}");

stubData.Id //returns 1
1
  • 1
    I haven't tested this, but it should be easy to self-answer just by running it, no? Commented Nov 24, 2011 at 9:38

2 Answers 2

1

Yes. OnDeserialized is supported. See the documentation

Here's a working cs-script example. Needs Newtonsoft.Json.dll is the same directory

//css_ref Newtonsoft.Json.dll

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

public class StubData:StubBase {}

public class StubBase {
    public string Id { get; set; }

    [OnDeserialized]
    public void OnDeserialized(StreamingContext context) {
        Id = "1";
    }
}

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
      var stubData = JsonConvert.DeserializeObject<StubData>(@"{""anyData"":""Foo""}");
      Console.WriteLine(stubData.Id); //returns 1
    }
}

This prints

1
Sign up to request clarification or add additional context in comments.

2 Comments

But the attribute is designated as Inherited = false. Doesn't that mean the attribute does not apply to derived objects?
I think he is asking for JSON.Net, not Newtonsoft...
0

After checking Table of differences between Newtonsoft.Json and System.Text.Json out, Callbacks are stated as Not supported, workaround, sample:

Newtonsoft.Json lets you execute custom code at several points in the serialization or deserialization process:

  • OnDeserializing (when beginning to deserialize an object)
  • OnDeserialized (when finished deserializing an object)
  • OnSerializing (when beginning to serialize an object)
  • OnSerialized (when finished serializing an object)

In System.Text.Json, you can simulate callbacks by writing a custom converter. The following example shows a custom converter for a POCO. The converter includes code that displays a message at each point that corresponds to a Newtonsoft.Json callback.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace SystemTextJsonSamples
{
    public class WeatherForecastCallbacksConverter : JsonConverter<WeatherForecast>
    {
        public override WeatherForecast Read(
            ref Utf8JsonReader reader,
            Type type,
            JsonSerializerOptions options)
        {
            // Place "before" code here (OnDeserializing),
            // but note that there is no access here to the POCO instance.
            Console.WriteLine("OnDeserializing");

            // Don't pass in options when recursively calling Deserialize.
            WeatherForecast forecast = JsonSerializer.Deserialize<WeatherForecast>(ref reader);

            // Place "after" code here (OnDeserialized)
            Console.WriteLine("OnDeserialized");

            return forecast;
        }

        public override void Write(
            Utf8JsonWriter writer,
            WeatherForecast forecast, JsonSerializerOptions options)
        {
            // Place "before" code here (OnSerializing)
            Console.WriteLine("OnSerializing");

            // Don't pass in options when recursively calling Serialize.
            JsonSerializer.Serialize(writer, forecast);

            // Place "after" code here (OnSerialized)
            Console.WriteLine("OnSerialized");
        }
    }
}

Register this custom converter by adding the converter to the Converters collection.

If you use a custom converter that follows the preceding sample:

  • The OnDeserializing code doesn't have access to the new POCO instance. To manipulate the new POCO instance at the start of deserialization, put that code in the POCO constructor.
  • Avoid an infinite loop by registering the converter in the options object and not passing in the options object when recursively calling Serialize or Deserialize.

For more information about custom converters that recursively call Serialize or Deserialize, see the Required properties section earlier in this article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.