Is it possible to set a field's value instead of replacing the reference with a new object when Deserializing with Gson?

Viewed 184

I'm currently using GSON to for my serialization library, and I'm having a some difficulty with figuring out how to read a value from the JSON and use it to update a field's value, without changing the reference of the field. I'll give a reduced example of my issue:

  public static class Property {
    String text;

    public Property(final String text) {
      this.text = text;
    }
  }

  public static class Delegate {
    final private Property property;

    public Delegate(final Property property) {
      this.property = property;
    }

    public String getValue() { return property.text; }

    public void setValue(final String value) {
      this.property.text = value;
    }
  }

  public static class ExampleWithPropertyAndDelegate {

    final Property textProp = new Property("Test");
    final Delegate delegate = new Delegate(textProp);
  }

So the way this works is that the delegate member of ExampleWithPropertyAndDelegate should always update the accompanied property member when setValue is called, e.g.


    /* create the object we want to serialize/deserialize properly*/
    var example = new ExampleWithPropertyAndDelegate();
    /* setting the value via the delegate should update the underlying Property reference*/
    example.delegate.setValue("Value Changed");

This example works exactly as expected when using the constructor. However, when deserializing, this no longer works. The underlying Property that the delegate references is no longer the same instance as the example.property member, that is example.property != example.delegate.property, even though it should.

I know this is happening since Gson uses the ReflectiveTypeAdapterFactory to actually set the object for the field, after construction. But is there a way to stop this from happening, say for any Property fields, instead of replacing them with a new instance via reflection, instead having it just set the value of the existing instance?

0 Answers
Related