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
522 stars 102 forks source link

Double quotes are not (always) escaped #297

Closed noandrea closed 9 years ago

noandrea commented 9 years ago

Hello, looks like that when an object get serialized/unserialized more than once, sometimes double quotes are not escaped.

Here is the code to replicate the error, i am using boon v0.30 and java 1.7.x

import java.util.HashMap;
import java.util.Map;

import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

public class MyObject {
    private String title;
    private Map<String, Object> attrs;
    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }
    /**
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }
    /**
     * @return the attrs
     */
    public Map<String, Object> getAttrs() {
        return attrs;
    }
    /**
     * @param attrs the attrs to set
     */
    public void setAttrs(Map<String, Object> attrs) {
        this.attrs = attrs;
    }

    public static void main(String[] args) {

        // create object
        MyObject myobject = new MyObject();
        // add a title and attributes
        myobject.setTitle("title with \" double qoutes");
        Map<String,Object> attributes = new HashMap<>();
        attributes.put("author", "author with \" double quotes");
        myobject.setAttrs(attributes);

        // create ObjectMapper
        ObjectMapper mapper = JsonFactory.create();

        // serialize myobject to json
        String myobjectJson = mapper.toJson(myobject);
        System.out.println(myobjectJson);
        // prints: {"title":"title with \" double qoutes","attrs":{"author":"author with \" double quotes"}}
        // parse myobjectJson to an object
        MyObject myobjectParsed = mapper.fromJson(myobjectJson, MyObject.class);

        // serialize again my myobjectParsed to json
        myobjectJson = mapper.toJson(myobjectParsed);
        System.out.println(myobjectJson);
        // double quoted for attrs are not escaped
        // prints: {"title":"title with \" double qoutes","attrs":{"author":"author with " double quotes"}}

        MyObject myobjectParsed2 = mapper.fromJson(myobjectJson,MyObject.class);
        // Exception in thread "main" org.boon.json.JsonException: expecting current character to be ':' but got '}' with an int value of 125

    }
}
RichardHightower commented 9 years ago

Thanks for posting the example. That will help me a lot. I will take a peek.

RichardHightower commented 9 years ago

Fixed. Sorry it took so long.