jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JsonWriter Fails to Write a Map with Long Keys

david316 opened this issue · comments

I am attempting to serialize a class containing a Map that has Longs as keys.
This is failing with
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
at com.cedarsoftware.util.io.JsonWriter.writeMapBody(JsonWriter.java:1982) ~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeMapWithStringKeys(JsonWriter.java:1972) ~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeImpl(JsonWriter.java:1012) ~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeField(JsonWriter.java:2154) ~[json-io-2.9.1.jar:?]

The method ensureJsonPrimitiveKeys returns true. It checks that the key is (o instanceof String || o instanceof Double || o instanceof Long || o instanceof Boolean)
but then method writeMapBody assumes the key is a String on line 1982

output.write((String) att2value.getKey());

David, this should be an easy fix (and easy test). I will create a 2.9.3
shortly.

On Mon, Feb 9, 2015 at 7:56 AM, david316 notifications@github.com wrote:

I am attempting to serialize a class containing a Map that has Longs as
keys.
This is failing with
java.lang.ClassCastException: java.lang.Long cannot be cast to
java.lang.String
at com.cedarsoftware.util.io.JsonWriter.writeMapBody(JsonWriter.java:1982)
~[json-io-2.9.1.jar:?]
at
com.cedarsoftware.util.io.JsonWriter.writeMapWithStringKeys(JsonWriter.java:1972)
~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeImpl(JsonWriter.java:1012)
~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeField(JsonWriter.java:2154)
~[json-io-2.9.1.jar:?]

The method ensureJsonPrimitiveKeys returns true. It checks that the key is
(o instanceof String || o instanceof Double || o instanceof Long || o
instanceof Boolean)
but then method writeMapBody assumes the key is a String on line 1982

output.write((String) att2value.getKey());


Reply to this email directly or view it on GitHub
#35.

David, the issue you reported has been fixed. Json-io 2.9.3 has been built
and released to Maven Central at 8:45am Eastern Standard Time. Within
about 2-3 hours of this, it should be available for usage in your pom file.

On Mon, Feb 9, 2015 at 7:59 AM, John DeRegnaucourt jdereg@gmail.com wrote:

David, this should be an easy fix (and easy test). I will create a 2.9.3
shortly.

On Mon, Feb 9, 2015 at 7:56 AM, david316 notifications@github.com wrote:

I am attempting to serialize a class containing a Map that has Longs as
keys.
This is failing with
java.lang.ClassCastException: java.lang.Long cannot be cast to
java.lang.String
at
com.cedarsoftware.util.io.JsonWriter.writeMapBody(JsonWriter.java:1982)
~[json-io-2.9.1.jar:?]
at
com.cedarsoftware.util.io.JsonWriter.writeMapWithStringKeys(JsonWriter.java:1972)
~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeImpl(JsonWriter.java:1012)
~[json-io-2.9.1.jar:?]
at com.cedarsoftware.util.io.JsonWriter.writeField(JsonWriter.java:2154)
~[json-io-2.9.1.jar:?]

The method ensureJsonPrimitiveKeys returns true. It checks that the key
is (o instanceof String || o instanceof Double || o instanceof Long || o
instanceof Boolean)
but then method writeMapBody assumes the key is a String on line 1982

output.write((String) att2value.getKey());


Reply to this email directly or view it on GitHub
#35.