Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't parse Json String with Moshi

I'm trying to parse a Json response with Moshi, the problem that I have is that the value of the key is a Json wrap on a String:

{"mobile_accounts_account_showProperties":"[{\"name\": \"current\"},{\"name\": \"available\"}]"}

This is my class

@Json(name = "mobile_accounts_account_showProperties")
private List<PropertyConfig> showProperties;

I try removing the ("") with a replace("\"[", "[") and a replace("\\", "") before parsing but is not an option because that remove some other quotes that I do need. I try to use JsonAdapter but I can't make that work. The JsonAdapter is not getting call.

public class PropertyConfigJsonAdapter {

public PropertyConfigJsonAdapter() {
}

@ToJson
String toJson(List<PropertyConfig> card) {
    return "";
}

@FromJson
List<PropertyConfig> fromJson(String object) {

    return new ArrayList<>();
}

I try this to see if the JsonAdapter was getting call but it never call the "fromJson" method. Here is how I call the adapter:

MoshiConverterFactory.create(new Moshi.Builder().add(new PropertyConfigJsonAdapter()).build())
like image 765
Ezequiel Di Sisto Avatar asked Nov 06 '22 23:11

Ezequiel Di Sisto


1 Answers

The easiest way is going to be to read in the value as a string and decode it from there. You can make this transparent to your application code with a JsonAdapter, like the ShowPropertiesContainer.ADAPTER below.

public final class ShowPropertiesContainer {
  public final List<PropertyConfig> showProperties;

  public static final Object ADAPTER = new Object() {
    @FromJson public ShowPropertiesContainer fromJson(JsonReader reader,
        JsonAdapter<ShowPropertiesContainer> fooAdapter,
        JsonAdapter<List<PropertyConfig>> propertyConfigListAdapter) throws IOException {
      ShowPropertiesContainer showPropertiesContainer = fooAdapter.fromJson(reader);
      return new ShowPropertiesContainer(propertyConfigListAdapter.fromJson(
          showPropertiesContainer.mobile_accounts_account_showProperties),
          showPropertiesContainer.mobile_accounts_account_showProperties);
    }
  };

  final String mobile_accounts_account_showProperties;

  ShowPropertiesContainer(List<PropertyConfig> showProperties,
      String mobile_accounts_account_showProperties) {
    this.showProperties = showProperties;
    this.mobile_accounts_account_showProperties = mobile_accounts_account_showProperties;
  }
}

public final class PropertyConfig {
  public final String name;

  PropertyConfig(String name) {
    this.name = name;
  }
}

@Test public void showPropertiesContainer() throws IOException {
  Moshi moshi = new Moshi.Builder()
      .add(ShowPropertiesContainer.ADAPTER)
      .build();
  JsonAdapter<ShowPropertiesContainer> adapter = moshi.adapter(ShowPropertiesContainer.class);
  String encoded =
      "{\"mobile_accounts_account_showProperties\":\"[{\\\"name\\\": \\\"current\\\"},"
          + "{\\\"name\\\": \\\"available\\\"}]\"}";
  ShowPropertiesContainer showPropertiesContainer = adapter.fromJson(encoded);
}
like image 198
Eric Cochran Avatar answered Nov 29 '22 09:11

Eric Cochran