#GSON - Expected BEGIN_OBJECT but was STRING

1 messages · Page 1 of 1 (latest)

bleak vector
#

Hey having a problem with Json & Gson and hoping someone might have a little more knowledge:

I'm trying to convert a string (json) to an object using GSON.fromJson(json, Grant.class)

My string of json is: {"id":"a31ed","rank":"merchant","scope":"earth","createdAt":1694437862922,"duration":1814400000}

And the stack is:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING

I've been looking at stackoverflow for answers for over 2 hours but nothing has helped

grim copper
#

json looks fine

#

Provide your group class

stiff ore
#

Try making a GsonDeserializer for your needs. This object is simple but maybe for scalability and maybe it'll even help get rid of the error

#

Creating one is super easy

#

If I find one I've made, I can send you an example

bleak vector
#

Whoops, it's Grant.class not Group

I'm using an adapter to only serialize/deserialze certain values of the Grant:

public class GrantAdapter implements JsonSerializer<Grant>, JsonDeserializer<Grant> {

    @Override
    public JsonElement serialize(Grant grant, Type type, JsonSerializationContext jsonSerializationContext) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", grant.getDocumentID());
        jsonObject.addProperty("rank", grant.getRank().getId());
        jsonObject.addProperty("scope", grant.getScope().getId());
        jsonObject.addProperty("createdAt", grant.getCreatedAt());
        jsonObject.addProperty("duration", grant.getDuration());
        return jsonObject;
    }

    @Override
    public Grant deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        JsonObject object = jsonElement.getAsJsonObject();
        String id = object.get("id").getAsString();
        String rank = object.get("rank").getAsString();
        String scope = object.get("scope").getAsString();
        long createdAt = object.get("createdAt").getAsLong();
        long duration = object.get("duration").getAsLong();

        CorePlugin plugin = CorePlugin.getInstance();
        Rank finalRank = plugin.getRankManager().getRankById(rank);
        Scope finalScope = plugin.getScopeManager().getScopeById(scope);

        if (finalRank == null || finalScope == null) {
            return null;
        }

        return new Grant(id, finalRank, finalScope, createdAt, duration);
    }
}
stiff ore
#

Oh you already have a deserializer

grim copper
#

I'd prob just do it by hand

#

oh wait this is not like mongo's codec

stiff ore
#

Well your deserializer looks fine

#

Did you add it to your Gson?

#

E.g. my Gson initializition in one of the projects is

  private void initJSON() {
    gson = GsonBuilder()
      .setPrettyPrinting()
      .registerTypeAdapter(Values.class, new ConfigurationDeserializer())
      .registerTypeAdapter(Messages.class, new MessagesDeserializer())
      .registerTypeAdapter(MinesManager.class, new MinesDeserializer(this))
      .registerTypeAdapter(MinesManager.class, new MinesSerializer())
      .setLenient()
      .create();
  }
grim copper
#

lol kotlin

bleak vector
#

So just to make sure:

            Gson GSON = new GsonBuilder()
                    .registerTypeAdapter(Grant.class, GrantAdapter.class)
                    .create();
stiff ore
#

Fixed it for those who don't know kotlin

#

xD

stiff ore
#

Otherwise it looks like you're calling a class

#

Wait no

#

Not GrantAdapter.class I don't think

#

It should be an instance of it

#

new GrantAdapter()

#

Actually both seem to be possible

#

Try both

bleak vector
#

@stiff ore Thank you so much! I've been banging my head against a wall the past 2 hours 😆
I was using @JsonAdapter(GrantAdapter.class) on the Grant.class which worked on another project for me, anyway glad that's fixed 🙂

stiff ore
#

😄

#

Happy coding!