Moshi: alternate name support in @Json like Gson

Created on 12 May 2017  路  13Comments  路  Source: square/moshi

It seems to be that alternate name is not supported yet?

Most helpful comment

Something like this could work.

final class Foo {
  final String username;

  Foo(String username) {
    this.username = username;
  }

  static final Object JSON_ADAPTER = new Object() {
    @FromJson private Foo fromJson(FooIntermediate intermediate) {
      if (intermediate.username != null) return new Foo(intermediate.username);
      if (intermediate.name != null) return new Foo(intermediate.name);
      throw new JsonDataException("No username or name found.");
    }
  };

  private static final class FooIntermediate {
    @Nullable final String username;
    @Nullable final String name;

    FooIntermediate(@Nullable String username, @Nullable String name) {
      this.username = username;
      this.name = name;
    }
  }
}

Don't forget to register the adapter when building your Moshi instance.
new Moshi.Builder().add(Foo.JSON_ADAPTER)

Stack Overflow is a good place to get help on those questions. (There's a moshi tag.)

Closing, since I don't think Moshi will add anything new for this feature.

All 13 comments

If I understand your feature request correctly, this is already supported.
Example:

final class Foo {
  @Json(name = "bar") final String foo = "hi";
}
{"bar":"hi"}

I think @msdx is talking about this feature.

For example:

class Foo {
    @Json(name = "username", alternate={"name"})
    final String username;
}
{"username": "Jim"}
{"name": "Jim"}

@egor-n Yes.
My English is not good.
This feature is what I want.

This is a feature I would like to see in Moshi as well but I would also like to contribute. Before I actually code something, I want to run it be you guys to make sure the solution makes sense.

I checked how GSON implements this functionality and it looks like it accommodates an array of Strings in SerializedName interface. Does it make sense to have something similar in Json interface in Moshi?

I don鈥檛 think this feature is a good fit for Moshi. If you need it, consider writing a custom adapter?

Oh! Okay. Fair enough.

May I ask why this wouldn't be a good fit? I understand it doesn't happen often but curious to know the reasoning.

In this case it's easy enough to write an adapter that handles your input and transforms it into your desired format.

In general we鈥檙e trying to build a small library that satisfies regular use cases well. Weird use cases exist, but usually it's better to fix the producer rather than to work around it in the consumer.

Hi @swankjesse

Can you please let me know with an example?

Here is my json

{"user_name", "dinesh"}
{"name", "dinesh"}
{"name_of_the_user", "dinesh"}

I want to parse these json using single class

class NameJson
{
    String user_name;
}

Moshi moshi = new Moshi.Builder().build();
NameJson detailsJson = moshi.adapter(NameJson.class).fromJson(json);

Can you please help me to understand the custom adapter usage here?

You'll need to create a class with all three fields, then use a @ToJson method to convert instances of that type to instances of your model type.

@aadithyabk I don't know how to implement this functionality by write a custom adapter, have you solved this problem?

Something like this could work.

final class Foo {
  final String username;

  Foo(String username) {
    this.username = username;
  }

  static final Object JSON_ADAPTER = new Object() {
    @FromJson private Foo fromJson(FooIntermediate intermediate) {
      if (intermediate.username != null) return new Foo(intermediate.username);
      if (intermediate.name != null) return new Foo(intermediate.name);
      throw new JsonDataException("No username or name found.");
    }
  };

  private static final class FooIntermediate {
    @Nullable final String username;
    @Nullable final String name;

    FooIntermediate(@Nullable String username, @Nullable String name) {
      this.username = username;
      this.name = name;
    }
  }
}

Don't forget to register the adapter when building your Moshi instance.
new Moshi.Builder().add(Foo.JSON_ADAPTER)

Stack Overflow is a good place to get help on those questions. (There's a moshi tag.)

Closing, since I don't think Moshi will add anything new for this feature.

I've went through this issue in an article here: https://medium.com/@mrnj92/goodbye-gson-hello-moshi-4e591116231e

Was this page helpful?
0 / 5 - 0 ratings