boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

Issue when serializing classes #378

Open MattAltermatt opened 7 years ago

MattAltermatt commented 7 years ago

Example is below, this gist of it is that when serializing a class the '}' is missing, which causes incorrect json and issues when later deserializing.

`
@Test public void testClassSerialization(){ String json = Boon.toJson(new MyModel(new KeyValueFinderImpl(), 100)); puts(json); // Missing the '}' after 'KeyValueFinderImpl"' //{"keyValueFinder":{"class":"org.boon.json.JsonParserAndMapperBaseTest$KeyValueFinderImpl","size":100}

    MyModel myModel = Boon.fromJson(json, MyModel.class);
    puts(myModel.getSize()); // returns 0, should be 100

    // manually placing the }
    String correctJson = "{\"keyValueFinder\":{\"class\":\"org.boon.json.JsonParserAndMapperBaseTest$KeyValueFinderImpl\"},\"size\":100}";

    myModel = Boon.fromJson(correctJson, MyModel.class);
    puts(myModel.getSize()); // returns 100 now
}

public static class MyModel {
    private KeyValueFinder keyValueFinder;
    private int size;

    public MyModel() {
    }

    public MyModel(KeyValueFinder keyValueFinder, int size) {
        this.keyValueFinder = keyValueFinder;
        this.size = size;
    }

    public KeyValueFinder getKeyValueFinder() {
        return keyValueFinder;
    }

    public void setKeyValueFinder(KeyValueFinder keyValueFinder) {
        this.keyValueFinder = keyValueFinder;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

public interface KeyValueFinder{
    int getValue(String key);
}

public static class KeyValueFinderImpl implements KeyValueFinder{

    @Override
    public int getValue(String key) {
        return key.hashCode();
    }
}

`