Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapCollectionType parse and serialize assume type String.class as the key #153

Open
trevjonez opened this issue Apr 15, 2016 · 2 comments
Open

Comments

@trevjonez
Copy link
Contributor

Given the following simple class

@JsonObject
public class JsonRpcRequests {
  @JsonField
  Map<Class, ApiRequest> requests;
}

The following mapper code is generated

public final class JsonRpcRequests$$JsonObjectMapper extends JsonMapper<JsonRpcRequests> {
  private static final JsonMapper<ApiRequest> COM_TREVJONEZ_RETRORX_BUSY_API_APIREQUEST__JSONOBJECTMAPPER = LoganSquare.mapperFor(ApiRequest.class);

  private static TypeConverter<Class> java_lang_Class_type_converter;

  @Override
  public JsonRpcRequests parse(JsonParser jsonParser) throws IOException {
    JsonRpcRequests instance = new JsonRpcRequests();
    if (jsonParser.getCurrentToken() == null) {
      jsonParser.nextToken();
    }
    if (jsonParser.getCurrentToken() != JsonToken.START_OBJECT) {
      jsonParser.skipChildren();
      return null;
    }
    while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
      String fieldName = jsonParser.getCurrentName();
      jsonParser.nextToken();
      parseField(instance, fieldName, jsonParser);
      jsonParser.skipChildren();
    }
    return instance;
  }

  @Override
  public void parseField(JsonRpcRequests instance, String fieldName, JsonParser jsonParser) throws IOException {
    if ("requests".equals(fieldName)) {
      if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) {
        HashMap<String, ApiRequest> map1 = new HashMap<String, ApiRequest>();
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
          String key1 = jsonParser.getText();
          jsonParser.nextToken();
          if (jsonParser.getCurrentToken() == JsonToken.VALUE_NULL) {
            map1.put(key1, null);
          } else {
            map1.put(key1, COM_TREVJONEZ_RETRORX_BUSY_API_APIREQUEST__JSONOBJECTMAPPER.parse(jsonParser));
          }
        }
        instance.requests = map1;
      } else {
        instance.requests = null;
      }
    }
  }

  @Override
  public void serialize(JsonRpcRequests object, JsonGenerator jsonGenerator, boolean writeStartAndEnd) throws IOException {
    if (writeStartAndEnd) {
      jsonGenerator.writeStartObject();
    }
    final Map<String, ApiRequest> lslocalrequests = object.requests;
    if (lslocalrequests != null) {
      jsonGenerator.writeFieldName("requests");
      jsonGenerator.writeStartObject();
      for (Map.Entry<String, ApiRequest> entry1 : lslocalrequests.entrySet()) {
        jsonGenerator.writeFieldName(entry1.getKey().toString());
        if (entry1.getValue() != null) {
          COM_TREVJONEZ_RETRORX_BUSY_API_APIREQUEST__JSONOBJECTMAPPER.serialize(entry1.getValue(), jsonGenerator, true);
        }
      }
      jsonGenerator.writeEndObject();
    }
    if (writeStartAndEnd) {
      jsonGenerator.writeEndObject();
    }
  }

  private static final TypeConverter<Class> getjava_lang_Class_type_converter() {
    if (java_lang_Class_type_converter == null) {
      java_lang_Class_type_converter = LoganSquare.typeConverterFor(Class.class);
    }
    return java_lang_Class_type_converter;
  }
}

The processor generates the serializing and parsing code with the assumption that the type will be of String.class ( Here, Here & Here )

It seems as though any provided or default TypeConverter<T> should be dispatched for the purpose of serialization here, as apposed to the current .toString() approach.

Based on the unused methods in the generated code and the logic here, it would seem like this was your eventual intent.

I plan to work on a pull request for this in the morning however if my understanding of the issue and speculation of your intent are wrong please let me know at your earliest convenience.

@trevjonez
Copy link
Contributor Author

It would seem that all of this is far less flexible than I would have hoped.
I am going to play with just simply doing a custom type converter and see where I can get in that direction.

@ste71m
Copy link

ste71m commented Jun 21, 2023

Same issue.
In Jackson you can declare what to use as key
@JsonSerialize(keyUsing = MyPairSerializer.class)
Map<MyPair, String> map

Don't know here' im new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants