0

I have a property like this:

    @CatalogExportField(columnName = "K", headerName = "catalog name")
private Boolean mpAvailable;

I need to get this as string while parsing in other class

   private CatalogExportDto convert(Variant variant, boolean willHaveProductTypeFields) {
    CatalogExportDto dto = new CatalogExportDto()


        .setMpAvailable(variant.isMpAvailable())

But here it is boolean.

but could not find any proper example.

this is also

catalog export field.java


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface CatalogExportField {

    String color() default "#56aaff";

    String columnName() default "";

    String headerName() default "";

    String displayName() default "";
}

After searching, i did some and got error:

Problem deserializing property 'MpAvailable' (expected type: [simple type, class java.lang.Boolean]; actual type: java.lang.String), problem: argument type mismatch at [Source: java.io.FileInputStream@1f40904; line: 1, column: 720] (through reference chain: java.util.ArrayList[0]->domain.util.CatalogExportDto["MpAvailable"])

for

this is in catalogexport dto

    @JsonDeserialize(using = BooleanDeserializer.class)
@JsonProperty("MpAvailable")
@CatalogExportField(columnName = "K", headerName = "catalog.export.mp_available")
private Boolean mpAvailable;

this is deserializer

public class BooleanDeserializer extends JsonDeserializer<String> {
protected static final String NO = "no";
protected static final String YES = "yes";

@Override
public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken currentToken = jp.getCurrentToken();

    if (currentToken.equals(JsonToken.VALUE_FALSE)) {
        return NO;

    }
        return YES;
}

}

I changed to this now

public class YesNoBooleanSerializer extends JsonSerializer {

protected static final String NO = "no";
protected static final String YES = "yes";

@Override
public void serialize(Boolean b, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
    jsonGenerator.writeString(b ? NO : YES);
}

}

} }

field

@JsonSerialize(using = YesNoBooleanSerializer.class)
@CatalogExportField(columnName = "K", headerName = "catalog.export.mp_available")
private Boolean mpAvailable;

i got error

Can not deserialize value of type java.lang.Boolean from String "no": only "true" or "false" recognized at [Source: java.io.FileInputStream@1d1ec976; line: 1, column: 534] (through reference chain: java.util.ArrayList[0]->CatalogExportDto["mpAvailable"])
mark
  • 727
  • 3
  • 14
  • 35

1 Answers1

1

A couple unordered thougts:

  • when you write JsonDeserializer<String>, probably you say that it is going to be your class which deserializes String-s from JSON. Actually you want to deserialize to either some weird own type, or to Boolean, but not to String, that is sure
  • if you serialize by writing "yes" or "no", you would deserialize by comparing to "yes" or "no". But you compare to JsonToken.VALUE_FALSE, which is most likely "false" or something
  • however your deserialize method probably did not run at all: as it presumably gets some "yes"/"no" text, and compares it to "false", the comparison would fail, so the method would return YES ("yes"), while the error message complains about getting a "no"

I know nothing about Spring or Boot, but writing

public class BooleanDeserializer extends JsonDeserializer<Boolean> {
protected static final String NO = "no";
protected static final String YES = "yes";

  @Override
  public Boolean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    JsonToken currentToken = jp.getCurrentToken();
    return currentToken.equals(YES);
  }
}

might be a better start. However I really do not know if this would be invoked at all, as JSON already has its own Boolean deserializer for sure. According to Right way to write JSON deserializer in Spring or extend it you may rather want to use this deserialization stuff for your own classes.

Community
  • 1
  • 1
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • java.lang.ThreadLocal@1d9a65d3 as output it gave. Not no or yes :( @tevemadar – mark May 22 '17 at 14:21
  • @mark probably I do not understand the goal. So far what I see is that you have a Boolean field somewhere, and for some reason you serialized it as 'yes' or 'no' in the JSON, but can not read it back (deserialize). However now your comment may mean that the JSON itself contains that stringified thread reference. In that case the serialize part has to be revisited, and deserializing lies way ahead. – tevemadar May 23 '17 at 04:44
  • I want to use yes or no, not true or false. value is boolean but i want to use it as yes or no – mark May 23 '17 at 05:00