-1

I need to convert a json string to particular type based on the function's return type. The json string could also be a plain string. I am facing issues while converting the strings using gson.fromJson method.

Why is an exception thrown in gson.fromJson if the string contains a space? And how do I get around this?

import java.lang.reflect.Method;

import com.google.gson.Gson;

class Trial {
    Object retu() {
        return "BEVERLY OUTLAW";
    }
}

public class sample {
    public static void main (String args[]) {
        Gson gson = new Gson();
        Trial obj = new Trial();

        Method func = obj.getClass().getDeclaredMethods()[0];
        Object o = gson.fromJson("beverlyoutlaw", func.getGenericReturnType());
        System.out.println(o); ---> this prints beverlyoutlaw
        o = gson.fromJson("beverly outlaw", func.getGenericReturnType()); ---> This throws an exception!
        System.out.println(o);
    }
}
user2761431
  • 925
  • 2
  • 11
  • 26
  • 1
    The answer is pretty simple: it's questionable wether a single string is valid Json or not, since two different RFCs specifying Json differ on this (more: https://stackoverflow.com/questions/13318420/is-a-single-string-value-considered-valid-json ). Gson does not consider a string valid Json and needs the outermost element to be either an object or an array. – Johannes H. Apr 30 '20 at 23:24
  • why does it work on Strings without spaces then? – user2761431 Apr 30 '20 at 23:26
  • I have to admit, I missed that part. Have you tried `gson.fromJson("\"beverly outlaw\"", ...)`? I'm pretty sure strings in Json need to be enclosed by quotes, even without spaces, so it's already "best effort" to even parse strings without them at all. – Johannes H. Apr 30 '20 at 23:28
  • Yes that works! – user2761431 Apr 30 '20 at 23:43
  • You're kind of going out of your way here to try to break things: The non JSON string passed to `fromJson`, the target type retrieved through `getGenericReturnType`. What are you trying to achieve? – Sotirios Delimanolis May 01 '20 at 00:43
  • I need to store and retrieve function outputs as a json. While retrieving I convert the output based on the return type of the function. This is part of the use case. Since a quoted string is a valid json [atleast jsonlint doesn't complain], I stored as a simple string, but ran into the issue I pointed out. – user2761431 May 01 '20 at 02:04

1 Answers1

0

Strings in Json need to be surrounden by quotes. That is - in the Json itself!

Neither "beverlyoutlaw" nor "beverly outlaw" contain quotes in the string - the quotes are just part of the Java String literal.

Apparently Gson does not enforce this for strings without spaces - for strings with spaces however it is strictly necessary.

To fix your issue, use o = gson.fromJson("\"beverly outlaw\"", func.getGenericReturnType());

Johannes H.
  • 5,875
  • 1
  • 20
  • 40
  • This is not quite right _for strings with spaces however it is strictly necessary_. The exception is thrown because Gson hasn't consumed the full input string, not because what it considered a string contained a space. – Sotirios Delimanolis May 01 '20 at 00:42
  • You're correct, @SotiriosDelimanolis. However in the end it means the same thing, just put the other way around: if Gson has not consumed the entire input when it stops parsing the string, but your entire json is a single string with a space, then quotes are needed in order for Gson to parse your Json entirely as that exact single string. – Johannes H. May 02 '20 at 03:44