java - Where the registerModule code should live when using jackson -
this test code.
dynabean bean1 = new lazydynabean(); bean1.set("p1", 1); bean1.set("p2", 2); objectmapper mapper = new objectmapper(); try { logger.error(mapper.writevalueasstring(bean1)); } catch (jsonprocessingexception ex) { logger.catching(ex); } simplemodule simplemodule = new simplemodule("simplemodule", new version(1, 0, 0, null, "com.bolan", "oa")); simplemodule.addserializer(dynabean.class, new dynabeanserializer()); mapper.registermodule(simplemodule); dynabean bean2 = new lazydynabean(); bean2.set("p1", 1); bean2.set("p2", 2); try { logger.error(mapper.writevalueasstring(bean2)); } catch (jsonprocessingexception ex) { logger.catching(ex); }
this source of dynabeanserializer:
public class dynabeanserializer extends jsonserializer<dynabean> { private final static logger logger = logmanager.getlogger(dynabeanserializer.class); @override public void serialize(dynabean value, jsongenerator gen, serializerprovider serializers) throws ioexception, jsonprocessingexception { logger.info("serializing byna bean!"); gen.writestartobject(); dynaproperty[] dynaproperties = value.getdynaclass().getdynaproperties(); (dynaproperty dynaproperty : dynaproperties) { try { gen.writeobjectfield(dynaproperty.getname(), propertyutils.getproperty(value, dynaproperty.getname())); } catch (illegalaccessexception | invocationtargetexception | nosuchmethodexception ex) { logger.error(ex); } } gen.writeendobject(); } @override public class<dynabean> handledtype() { logger.info("get jsonserializer type!"); return dynabean.class; } }
and output is:
10:18:09.794 [javafx application thread] error com.bolan.test.javafx.test.fxmlcontroller - {"dynaclass":{"dynabeanclass":"org.apache.commons.beanutils.lazydynabean","name":"org.apache.commons.beanutils.lazydynaclass","restricted":false,"returnnull":false,"dynaproperties":[{"name":"p1","type":"java.lang.integer","contenttype":null,"indexed":false,"mapped":false},{"name":"p2","type":"java.lang.integer","contenttype":null,"indexed":false,"mapped":false}]},"map":{"p2":2,"p1":1}} 10:18:09.797 [javafx application thread] error com.bolan.test.javafx.test.fxmlcontroller - {"dynaclass":{"dynabeanclass":"org.apache.commons.beanutils.lazydynabean","name":"org.apache.commons.beanutils.lazydynaclass","restricted":false,"returnnull":false,"dynaproperties":[{"name":"p1","type":"java.lang.integer","contenttype":null,"indexed":false,"mapped":false},{"name":"p2","type":"java.lang.integer","contenttype":null,"indexed":false,"mapped":false}]},"map":{"p2":2,"p1":1}}
however wish output this:
10:21:03.933 [javafx application thread] error com.bolan.test.javafx.test.fxmlcontroller - {"p1":1,"p2":2}
what's more, if edit test code following:
dynabean bean1 = new lazydynabean(); bean1.set("p1", 1); bean1.set("p2", 2); objectmapper mapper = new objectmapper(); simplemodule simplemodule = new simplemodule("simplemodule", new version(1, 0, 0, null, "com.bolan", "oa")); simplemodule.addserializer(dynabean.class, new dynabeanserializer()); mapper.registermodule(simplemodule); dynabean bean2 = new lazydynabean(); bean2.set("p1", 1); bean2.set("p2", 2); try { logger.error(mapper.writevalueasstring(bean2)); } catch (jsonprocessingexception ex) { logger.catching(ex); }
the output wish~ it's very strange.
you must register modules going use before using objectmapper
first time. registration after usage may or may not have effect; adding custom (de)serializers types have serialized/deserialized not have effect due caching of serializers , deserializers.
Comments
Post a Comment