Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为自定义序列化bean生成json模式_Json_Jackson_Jsonschema_Fasterxml_Jackson Modules - Fatal编程技术网

为自定义序列化bean生成json模式

为自定义序列化bean生成json模式,json,jackson,jsonschema,fasterxml,jackson-modules,Json,Jackson,Jsonschema,Fasterxml,Jackson Modules,我正在使用FasterXML从POJO构建json模式。 在我的POJO使用自定义json序列化之前,一切都正常。在我的例子中,我有一个类型为org.joda.Money的字段,并用相应的joda模块将其序列化。这个bean是串行化的,就像 { "amount": "...", "currency": "..." } 但其模式如下所示: { "type" : "object", "id" : "urn:jsonschema:org:joda:money:Money", "p

我正在使用FasterXML从POJO构建json模式。 在我的POJO使用自定义json序列化之前,一切都正常。在我的例子中,我有一个类型为org.joda.Money的字段,并用相应的joda模块将其序列化。这个bean是串行化的,就像

{
  "amount": "...",
  "currency": "..."
}
但其模式如下所示:

{
  "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"
        }
      }
    }
  }
}

有没有办法自定义生成的模式?

我认为
jackson数据类型joda
不支持joda Money数据类型

你可以试试我自己的模块。虽然我不确定它是否打算使用模式生成,但它在序列化/反序列化方面工作得很好

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);
        }
    }
} 
公共类JodaMoneyModule扩展了SimpleModule{
@凌驾
公共无效设置模块(最终设置上下文){
最终SimpleSerializers序列化程序=新SimpleSerializers();
addSerializer(Money.class,new JodaMoneySerializer());
addSerializers(序列化程序);
最终SimpleDeserializers反序列化程序=新SimpleDeserializers();
addDeserializer(Money.class,new JodaMoneyDeserializer());
addDeserializers(反序列化程序);
}
私有类JodaMoneySerializer扩展了JsonSerializer{
@凌驾
公共无效序列化(
最终货币价值,
最终JSONG发电机,
最终序列化程序(提供程序序列化程序)
抛出IOException{
gen.writeString(value==null?null:value.toString());
}
}
私有类JodaMoneyDeserializer扩展了JsonDeserializer{
@凌驾
公共资金反序列化(最终JSONP解析器,最终反序列化上下文ctxt)
抛出IOException{
最终字符串值=p.getValueAsString();
返回值==null?null:Money.parse(值);
}
}
} 

我想知道您使用哪个模块来序列化和反序列化Joda Money?这里是:
com.fasterxml.jackson.datatype jackson datatype Joda 2.4.4
我认为它不包括Joda Money模块。看看我的答案。