1

Closed

extend the WriteJson en ReadJson methods with the target object on the JsonConverter class.

description

public abstract class JsonConverter
{
public abstract void WriteJson(JsonWriter writer, object value, object target, JsonSerializer serializer);
public abstract object ReadJson(JsonReader reader, Type objectType, object existingValue, object target, JsonSerializer serializer);
 
...
 
You sometimes need the target to do some stuff
Closed Apr 9, 2012 at 4:26 AM by JamesNK
I don't want to do this. This is a very edge case use and making the change will break all existing JsonConverters.

comments

tkoster wrote Dec 9, 2010 at 4:59 AM

Define "target"...

dabuddhaman wrote Dec 9, 2010 at 3:42 PM

ex: so that you can pass the target if you ar serializing a property
private object CreateValueProperty(JsonReader reader, JsonProperty property, object target, bool gottenCurrentValue, object currentValue)
{
  JsonContract contract = GetContractSafe(property.PropertyType, currentValue);
  Type objectType = property.PropertyType;

  JsonConverter converter = GetConverter(contract, property.MemberConverter);

  if (converter != null && converter.CanRead)
  {
    if (!gottenCurrentValue && target != null && property.Readable)
      currentValue = property.ValueProvider.GetValue(target);

    return converter.ReadJson(reader, objectType, currentValue, target, GetInternalSerializer());
  }

  return CreateValueInternal(reader, objectType, contract, property, currentValue);
}

dabuddhaman wrote Dec 9, 2010 at 3:53 PM

In my proper case I need to know the owner because we are implementing lazy loading over the wire of NHibernate entities.
In the converter I need the target (the owner of the lazy/deferred entity or list) so I can extract its key (which is needed to do lazy loading in NHibernate).

dabuddhaman wrote Jan 14, 2011 at 8:58 AM

ex:

class Person
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Person> Children { get; set; }
}

When deserializing a Person, the person object is the target and the children property is the current value

dabuddhaman wrote Jan 14, 2011 at 9:19 AM

public class Person
{
public int Id {get; private set;}
public string Name {get; set;}
public IList<Person> Children {get; set;}
}

when (de)serializing a person, Json.NET arrives at the children property and our converter tries to get/set json form our property value.
The children property is the currentValue and the person object is the target.
So target is the object which contains the property that your converter wants to get/set.

dabuddhaman wrote Jan 14, 2011 at 9:22 AM

public class Person
{
public int Id {get; private set;}
public string Name {get; set;}
public IList<Person> Children {get; set;}
}

when (de)serializing a person, Json.NET arrives at the children property and our converter tries to get/set json form our property value.
The children property is the currentValue and the person object is the target.
So target is the object which contains the property that your converter wants to get/set.

dabuddhaman wrote Jan 14, 2011 at 9:38 AM

public class Person
{
public int Id {get; private set;}
public string Name {get; set;}
public IList<Person> Children {get; set;}
}

when (de)serializing a person, Json.NET arrives at the children property and our converter tries to get/set json form our property value.
The children property is the currentValue and the person object is the target.
So target is the object which contains the property that your converter wants to get/set.

dabuddhaman wrote Jan 14, 2011 at 9:39 AM

public class Person
{
public int Id {get; private set;}
public string Name {get; set;}
public IList<Person> Children {get; set;}
}

when (de)serializing a person, Json.NET arrives at the children property and our converter tries to get/set json form our property value.
The children property is the currentValue and the person object is the target.
So target is the object which contains the property that your converter wants to get/set.

dabuddhaman wrote Jan 14, 2011 at 9:42 AM

When using Nhibernate the Children property can be Lazy Loaded (not yet loaded from the DataBase)
When (de)serializing a lazy loaded property I need some additional information (which I will store as metadata in the json) and I need to access the target object

dabuddhaman wrote Oct 13, 2011 at 10:07 AM

Any progress?????????