devtagare / json-simple

Automatically exported from code.google.com/p/json-simple
Apache License 2.0
0 stars 0 forks source link

Quotes missing for non string types #63

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

It seems that the quotes are always missing if the object added to the 
JSONObject is not a String.

For instance the following code :

    public static enum Test { AZERTY, UIOP }
    public static void main(String[] args) throws IOException {
        JSONObject test = new JSONObject();
        test.put("test", Test.AZERTY);
        test.put("now", new Date());
        StringWriter out = new StringWriter();
        test.writeJSONString(out);
        System.out.println(out.toString());
    }

produces : 

{"now":Thu Jan 05 11:16:18 CET 2012,"test":AZERTY}

Which is not valid JSon. It should have been :

{"now":"Thu Jan 05 11:16:18 CET 2012","test":"AZERTY"}

with quotes.

The correction shouldn't be hard. If time permits I'm going to look at the 
source to see if I can help.

Regards,
Raphaël Lemaire

Original issue reported on code.google.com by lemaire....@gmail.com on 5 Jan 2012 at 10:18

GoogleCodeExporter commented 8 years ago
The correction is line 154 of org.json.simple.JSONValue.

Replace : 

out.write(value.toString());

By : 

out.write('\"');
out.write(value.toString());
out.write('\"');

Hope it helps.

Regards,

Original comment by lemaire....@gmail.com on 5 Jan 2012 at 11:22

GoogleCodeExporter commented 8 years ago
Only mappings described in the following page are supported:
http://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities

The purpose of not quoting toString() by default is to let user override it to 
provide their own literals, but it seems that user often has issues here. 
Considering making an enhancement on it.

Original comment by fangyid...@gmail.com on 21 Feb 2012 at 5:25

GoogleCodeExporter commented 8 years ago
I've experienced this in my own code - it would appear to make sense to make a 
special case for "instance of java.lang.Enum" and wrap the ".toString" value in 
quotes.

Original comment by ray.bellis on 11 Jul 2013 at 9:00

GoogleCodeExporter commented 8 years ago

Original comment by jon.cham...@gmail.com on 10 Aug 2013 at 4:56

GoogleCodeExporter commented 8 years ago
How is this a duplicate of #26? The following failing test case against version 
1.1.1 demonstrates the issue that I think Ray has been describing:

package json.simple.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Test;

public class JSONEnumTypeTest {
    private enum TestEnum{ VALUE1, VALUE2 };

    @Test
    public void testWriteEnum() throws IOException, ParseException {
        final String TEST_ENUM_KEY = "testEnum"; 
        TestEnum testEnum = TestEnum.VALUE1;
        Writer writer = new StringWriter(); 

        JSONObject jsonWithEnum = new JSONObject();
        jsonWithEnum.put(TEST_ENUM_KEY, testEnum);

        jsonWithEnum.writeJSONString(writer);

        Reader reader = new StringReader(writer.toString());

        JSONParser parser = new JSONParser();
        Object resultAsObject = parser.parse(reader);
        assertTrue(resultAsObject instanceof JSONObject);

        JSONObject resultAsJsonObject = (JSONObject) resultAsObject;
        String enumValue = (String) resultAsJsonObject.get(TEST_ENUM_KEY);
        TestEnum parsedTestEnum = TestEnum.valueOf(enumValue);

        assertEquals(testEnum, parsedTestEnum);
    }
}

And the following addition to JSONValue.writeJSONString would fix this test:

        if (value instanceof Enum) {
            out.write('\"');
            out.write(value.toString());
            out.write('\"');
            return;
        }

What's the rationale in not adopting this kind of patch?

Original comment by hthdjeut...@googlemail.com on 6 Aug 2015 at 3:56