Невозможно сериализовать вложенный объект с помощью парсера custon jackson

Я создал собственный сериализатор для своего класса java, который содержит множество вложенных объектов.

Мой мотив - добавить тип класса java в сериализованный объект JSON.

ex Мой тип класса - Студент, а созданный JSON -

      { id:1, name:abc }

Я хочу, чтобы это было как

      {id:1, name:abc, type:"Student "}

что не так уж и сложно. Моя проблема в том, что мой объект похож на

      {
  "description": null,
  "edit": [],
  "strategy": [
    {
      "description": null,
      "regions": {
        "region": []
      },
      "markets": {
        "market": [
          {},
          {}
        ]
      },
      "securityTypes": {},
      "parameter": [
        {
          "description": null,
          "enumPair": [],
          "name": "StartTime",
          "fixTag": 7602,
          "use": "REQUIRED",
          "mutableOnCxlRpl": true,
          "revertOnCxlRpl": null,
          "definedByFIX": false,
          "minValue": null,
          "maxValue": null,
          "constValue": null,
          "localMktTz": null
        },
        {},
        {},
        {}
      ],
      "edit": []
    }
  ],
  "draftFlagIdentifierTag": null,
  "changeStrategyOnCxlRpl": null,
  "imageLocation": null
}

и я хочу добавить тип к каждому параметру, а в остальном все должно быть сохранено как обычно сериализовано.

Я пробовал

      public class StrategiesSerializer extends StdSerializer<StrategiesT> {
    
    public StrategiesSerializer() {
        this(null);
    }

    public StrategiesSerializer(Class<StrategiesT> strategies) {
        super(strategies);
    }

    @Override
    public void serialize(StrategiesT value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        
        gen.writeStartObject();
        
        for(Field field: value.getClass().getDeclaredFields()) {
            System.out.println(field.getName());
            if(field.getName() != "strategy") provider.defaultSerializeField(field.getName(), field, gen);

            if(field.getName() == "strategy"){
                
                gen.writeArrayFieldStart("strategy");
                
                
                for(StrategyT strategy :  value.getStrategy()) {

                    for(Field strategyField : strategy.getClass().getDeclaredFields()) {
                        
                        provider.defaultSerializeField(strategyField.getName(), strategyField, gen);
                        
                    }
                    
                    gen.writeStartObject();
                    gen.writeStringField("type", strategy.getClass().getName());
                    gen.writeEndObject();
                }
                
                gen.writeEndArray();

            }
        }
        
        
        gen.writeEndObject();
    
    }
    
}

Это дает мне неправильный результат.

Please let me know if i can make myself more clear.

0 ответов

Другие вопросы по тегам