1

Is possible use regular expression for the jsonproperty annotation and how is it done?

I have json from third party who prefix properties like below:

{ 
   "1. information": "testing"
   "2. information": "testing 2"
}

I would like that all information get attach to list Array information, see below example of pseudo:

public class TimeSeries {
        @JsonProperty(/.*information/g)
        private List<String> information;
}

Sometimes some of String properties is also postfix with date, is possible, but have unique name besides that. Is possible to use regular expression here?

{ 
   "Price 2019-09-01": "testing"
   "Quantity 2019-09-01": "testing 2"
}
Dasma
  • 1,023
  • 13
  • 34
  • 2
    You should use `JsonAnySetter` annotation and process names manually in a method. Take a look at: [jackson xmlmapper for map , react for specific tag](https://stackoverflow.com/questions/57461455/jackson-xmlmapper-for-map-string-object-react-for-specific-tag/57468258#57468258), [How can I deserialize a JSON to a Java class which has known mandatory fields, but can have several unknown fields?](https://stackoverflow.com/questions/57081709/how-can-i-deserialize-a-json-to-a-java-class-which-has-known-mandatory-fields-b/57081808#57081808) – Michał Ziober Oct 11 '19 at 16:34

2 Answers2

2

I don't know if it is possible to put regex in @JsonProperty.

But you can use @JsonAnySetter for "information" fields.

Map<String, Object> informations = new LinkedHashMap<>();

@JsonAnySetter
void setInformation(String key, Object value) {
    informations.put(key, value);
}

Basically, you map all fields that have static names:

@JsonProperty("name")
String name;

and in informations you put all the others fields from json.

2

I found the answer through deserialize, see below example:

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
@NoArgsConstructor
public class TimeSeries {

    @JsonProperty("Meta Data")
    private MetaData metaData;

    @JsonDeserialize(using = MetaDataDeserializer.class)
    @Getter
    @Setter
    @NoArgsConstructor
    static class MetaData {
        private String information;
        private String symbol;
    }

    private static class MetaDataDeserializer extends StdDeserializer<MetaData> {
        protected MetaDataDeserializer() {
            super(MetaData.class);
        }

        @Override
        public MetaData deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException {
            MetaData metaData = new MetaData();
            while (!parser.isClosed()) {
                JsonToken jsonToken = parser.nextToken();

                if (JsonToken.FIELD_NAME.equals(jsonToken)) {
                    String fieldName = parser.getCurrentName();
                    System.out.println(fieldName);

                    //jsonToken =
                    parser.nextToken();

                    if (fieldName.toLowerCase().matches(".*information")) {
                        metaData.setInformation(parser.getValueAsString());
                    }
                }
            }

            return metaData;
        }
    }
}
Dasma
  • 1,023
  • 13
  • 34