2

I'm building json-schema from my POJO using FasterXML's json-schema generator. Everything goes OK until my POJO uses custom json serialization. In my case I have a field of type org.joda.Money and I serialize it with a corresponding joda-module. The bean is serialised well like

{
  "amount": "...",
  "currency": "..."
}

But its schema looks like this:

{
  "type" : "object",
  "id" : "urn:jsonschema:org:joda:money:Money",
  "properties" : {
    "amount" : {
      "type" : "number"
    },
    "amountMinorLong" : {
      "type" : "integer"
    },
    "scale" : {
      "type" : "integer"
    },
    "minorPart" : {
      "type" : "integer"
    },
    "positive" : {
      "type" : "boolean"
    },
    "amountMajor" : {
      "type" : "number"
    },
    "amountMinor" : {
      "type" : "number"
    },
    "amountMinorInt" : {
      "type" : "integer"
    },
    "positiveOrZero" : {
      "type" : "boolean"
    },
    "zero" : {
      "type" : "boolean"
    },
    "negative" : {
      "type" : "boolean"
    },
    "amountMajorLong" : {
      "type" : "integer"
    },
    "amountMajorInt" : {
      "type" : "integer"
    },
    "negativeOrZero" : {
      "type" : "boolean"
    },
    "currencyUnit" : {
      "type" : "object",
      "id" : "urn:jsonschema:org:joda:money:CurrencyUnit",
      "properties" : {
        "symbol" : {
          "type" : "string"
        },
        "numeric3Code" : {
          "type" : "string"
        },
        "countryCodes" : {
          "type" : "array",
          "items" : {
            "type" : "string"
          }
        },
        "code" : {
          "type" : "string"
        },
        "decimalPlaces" : {
          "type" : "integer"
        },
        "defaultFractionDigits" : {
          "type" : "integer"
        },
        "currencyCode" : {
          "type" : "string"
        },
        "pseudoCurrency" : {
          "type" : "boolean"
        },
        "numericCode" : {
          "type" : "integer"
        }
      }
    }
  }
}

Is there any way to customize the generated schema?

Alexey Pomelov
  • 196
  • 1
  • 9

1 Answers1

0

I don't think the jackson-datatype-joda supports the Joda Money data types.

You can give a try my own module. I'm not sure though if it is goging to work with the schema generation but it works fine for serialization/deserialization.

public class JodaMoneyModule extends SimpleModule {

    @Override
    public void setupModule(final SetupContext context) {
        final SimpleSerializers serializers = new SimpleSerializers();
        serializers.addSerializer(Money.class, new JodaMoneySerializer());
        context.addSerializers(serializers);
        final SimpleDeserializers deserializers = new SimpleDeserializers();
        deserializers.addDeserializer(Money.class, new JodaMoneyDeserializer());
        context.addDeserializers(deserializers);
    }

    private class JodaMoneySerializer extends JsonSerializer<Money> {
        @Override
        public void serialize(
                final Money value,
                final JsonGenerator gen,
                final SerializerProvider serializers)
                throws IOException {
            gen.writeString(value == null ? null : value.toString());
        }
    }

    private class JodaMoneyDeserializer extends JsonDeserializer<Money> {

        @Override
        public Money deserialize(final JsonParser p, final DeserializationContext ctxt)
                throws IOException {
            final String value = p.getValueAsString();
            return value == null ? null : Money.parse(value);
        }
    }
} 
Alexey Gavrilov
  • 10,593
  • 2
  • 38
  • 48