-1

I am trying to deserialize a message and I am getting an exception for the below statement.

Object key = gson.fromJson("40280381-3d27-5493-013d-4be146935c5c_abcdefg/ijklmnop", Class.forName("java.lang.String"));

The exception is occurring due to the / in the string value, if I remove the forward slash this code is working fine. But I do need the / in the string value, please help me out in this.

Actual Method

private void fillTheMap(JsonParser jp, Map<Object, Object> map) {
    JsonToken jt = jp.nextToken();
    while ((jt = jp.nextToken()) != JsonToken.END_ARRAY) {
        // Get the key.
        jt = jp.nextToken();

        String keyTypeName = jp.getCurrentName();
        keyTypeName = keyTypeName.replace(":mapKey", "");
        Class keyType = Class.forName(keyTypeName);

        jt = jp.nextToken();

        Object key = gson.fromJson(jp.getText(), keyType);

        Object value = deSerIze(jp);
        map.put(key, value);

        jt = jp.nextToken();
    }
}

Exception

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON

user2723039
  • 221
  • 2
  • 7
  • 16
  • 1
    `gson.fromJson`... The problem isn't the forward slash, it is the entire string... It isn't JSON – OneCricketeer Mar 28 '16 at 19:20
  • 1
    I'm confused what you want.. You are using Gson to convert a String into a String? Please show your actual code and JSON object in a [mcve] – OneCricketeer Mar 28 '16 at 19:25
  • 1
    No, not really. Do you not see what you are doing? You have a string already. Why are you trying to deserialize it with Gson? – OneCricketeer Mar 28 '16 at 19:30
  • A `JSONObject` is already like a `Map`. You can get and put values into both. All you want is do convert between the two? – OneCricketeer Mar 28 '16 at 19:43
  • @cricket_007 We have a custom deserializer to deserialize a .ser file, And the above code is to create a Map – user2723039 Mar 28 '16 at 19:58

1 Answers1

-1

As far as I know slash characters in JSON should be escaped. Try to replace / with \/.

And also you can use String.class instead of Class.forName("java.lang.String") scince it's more type-safe.

Denis Kokorin
  • 887
  • 8
  • 17