Issue 63 in json-simple: Quotes missing for non string types

20 views
Skip to first unread message

json-...@googlecode.com

unread,
Jan 5, 2012, 5:18:25 AM1/5/12
to json-...@googlegroups.com
Status: New
Owner: ----
Labels: Type-Defect Priority-Medium

New issue 63 by lemaire....@gmail.com: Quotes missing for non string types
http://code.google.com/p/json-simple/issues/detail?id=63

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




json-...@googlecode.com

unread,
Jan 5, 2012, 6:23:06 AM1/5/12
to json-...@googlegroups.com

Comment #1 on issue 63 by lemaire....@gmail.com: Quotes missing for non
string types
http://code.google.com/p/json-simple/issues/detail?id=63

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,

json-...@googlecode.com

unread,
Feb 21, 2012, 12:29:00 PM2/21/12
to json-...@googlegroups.com
Updates:
Status: Accepted
Labels: -Type-Defect Type-Enhancement

Comment #2 on issue 63 by fangyid...@gmail.com: Quotes missing for non
string types
http://code.google.com/p/json-simple/issues/detail?id=63

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.

json-...@googlecode.com

unread,
Jul 11, 2013, 5:00:50 AM7/11/13
to json-...@googlegroups.com

Comment #3 on issue 63 by ray.bellis: Quotes missing for non string types
http://code.google.com/p/json-simple/issues/detail?id=63

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.

--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

json-...@googlecode.com

unread,
Aug 10, 2013, 12:58:40 AM8/10/13
to json-...@googlegroups.com
Updates:
Status: Duplicate
Mergedinto: 26

Comment #4 on issue 63 by jon.cham...@gmail.com: Quotes missing for non
string types
http://code.google.com/p/json-simple/issues/detail?id=63

(No comment was entered for this change.)

json-...@googlecode.com

unread,
Aug 6, 2015, 11:56:40 AM8/6/15
to json-...@googlegroups.com

Comment #5 on issue 63 by hthdjeut...@googlemail.com: Quotes missing for
non string types
https://code.google.com/p/json-simple/issues/detail?id=63

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?
Reply all
Reply to author
Forward
0 new messages