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?