I recently came across the concept of using AutoValues (GSON) extension to speed up parsing our json data. We actuall found it parsed our json twice as fast.
But I am missing a point, the data set while using AutoValues is immutable, but often our data sets/models are similar to the json and there are no mappers required.
So if I want to set individual data elements in my model classes this does not look feasible(since we have only abstract getters and no setters. Is there a way around this problem or am I missing a point. I will explain using a sample code of what we do and the similar syntax using AutoValue.
So we usually write the code something like
public class ModelClass{
private String aValue;
private String anotherValue;
public String getAValue(){
return this.aValue;
}
public void setAValue(String aValue){
this.aValue = aValue;
}
public String getAnotherValue(){
return this.anotherValue;
}
public String setAnotherValue(anotherValue){
this.anotherValue = anotherValue
}
}
public class ViewClass{
public void foo(){
String json = {"aValue":"This is a sample string", "anotherValue": "this is another string"};
ModelClass model = gson.fromJson(json, ModelClass.class);
model.getAValue();
model.setAValue("this is a new value"); // and we can set individual element ModelClass.aValue
}
}
But while using Auto Value the structure of the Model Class changes to
@AutoValue public abstract class AutoValueModel{
public abstract String bValue();
public abstract String otherValue();
public static AutoValueModel(String bValue, String otherValue){
return new AutoValue_AutoValueModel(bValue, otherValue);
}
}
// Now as you can see AutoValueModel structure does not contain any setters what if I may want to change just the bValue (based on say a user action, even though I know that the basic premise behind AutoValues is them being immutable) and continue using in other parts of code. And then serialize a json using the very same AutoValueModel. Or should I deserialize using AutoValue technique and then use a mapped model on which I can perform changes to the dataset? (Will I lose the benefit of speed attained by using AutoValue if I use this technique?)
Also reference from where I learnt about AutoValues :