private static ObjectMapper createObjectMapper() { om = new ObjectMapper(); final SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null)); module.addDeserializer(MessageType.class, new MessageTypeDeserializer()); module.addDeserializer(RegisterMessageImpl.class, new RegisterDeserializer()); module.addSerializer(RegisterMessageImpl.class, new RegisterSerializer()); module.addDeserializer(RegisterResponseImpl.class, new RegisterResponseDeserializer()); module.addSerializer(RegisterResponseImpl.class, new RegisterResponseSerializer()); module.addDeserializer(HelloMessageImpl.class, new HelloDeserializer()); module.addSerializer(HelloMessageImpl.class, new HelloSerializer()); module.addDeserializer(HelloResponseImpl.class, new HelloResponseDeserializer()); module.addSerializer(HelloResponseImpl.class, new HelloResponseSerializer()); module.addDeserializer(AckMessageImpl.class, new AckDeserializer()); module.addSerializer(AckMessageImpl.class, new AckSerializer()); module.addDeserializer(PingMessageImpl.class, new PingDeserializer()); module.addSerializer(PingMessageImpl.class, new PingSerializer()); module.addDeserializer(NotificationMessageImpl.class, new NotificationDeserializer()); module.addSerializer(NotificationMessageImpl.class, new NotificationSerializer()); module.addDeserializer(UnregisterMessageImpl.class, new UnregisterDeserializer()); module.addSerializer(UnregisterMessageImpl.class, new UnregisterMessageSerializer()); module.addDeserializer(UnregisterResponseImpl.class, new UnregisterResponseDeserializer()); module.addSerializer(UnregisterResponseImpl.class, new UnregisterResponseSerializer()); om.registerModule(module); return om; }
public MarketBtce(Currency cur1, Currency cur2) throws ExchangeError { super(cur1, cur2, "btce"); // JSON mapper inialisation mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationConfig.Feature.USE_BIG_DECIMAL_FOR_FLOATS, false); // Ajout d'un deserializer d'ordre customis� pour passer de "[ prix, amount ]" dans le json � // "new Order(prix,amount,Type.XXX) en java" SimpleModule testModule = new SimpleModule("MyModule", new Version(1, 0, 0, null)); testModule.addDeserializer(Order.class, new OrderDeserializer()); mapper.registerModule(testModule); // Ajout d'un deserializer des types customis� pour passer de "ask"(resp. "bid") dans le json � // "Type.ASK" (resp. "Type.BID") en java testModule = new SimpleModule("MyModule2", new Version(1, 0, 0, null)); testModule.addDeserializer(Type.class, new TypeDeserializer(true)); mapper.registerModule(testModule); // Verification si la pair <cur1, cur2> est accepte par l'exchange URL fee_url; Fee f; try { fee_url = new URL( "https://btc-e.com/api/2/" + cur1.name().toLowerCase() + "_" + cur2.name().toLowerCase() + "/fee"); String is = Util.getData(fee_url); f = mapper.readValue(is, Fee.class); Assert.checkPrecond( f.error == null, "BTC-E n'autorise pas la pair: <" + cur1.name() + "," + cur2.name() + ">"); // On set les frais de transaction maintenant qu'on est sur que Btc-e autorise la paire // <cur1,cur2> fee_percent = Op.mult(f.trade, new Decimal("0.01")); } catch (JsonParseException e) { e.printStackTrace(); throw new ExchangeError("JsonParseException: Erreur jackson"); } catch (JsonMappingException e) { e.printStackTrace(); throw new ExchangeError("JsonMappingException: Erreur jackson"); } catch (IOException e) { e.printStackTrace(); throw new ExchangeError("IOException: Erreur jackson"); } }
private Records<Visit> deserializeVisits(String jsonInput) throws IOException { CustomVisitTypeDeserializer deserializer = new CustomVisitTypeDeserializer(); SimpleModule module = new SimpleModule("VisitTypeDeserializerModule", new Version(1, 0, 0, null)); module.addDeserializer(VisitType.class, deserializer); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(module); Records<Visit> records = mapper.readValue(jsonInput, new TypeReference<Records<Visit>>() {}); return records; }
public JsonContext() { ObjectMapper mapper = getObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper .getSerializationConfig() .addMixInAnnotations(ProtocolObject.class, StyxClassIdJsonMixIn.class); SimpleModule module = new SimpleModule("ProtocolDeserializer Module", new Version(1, 0, 0, null)); module.addDeserializer( HashMapMessage.class, new StdDeserializer<HashMapMessage>(HashMapMessage.class) { @Override @SuppressWarnings("unchecked") public HashMapMessage deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = (ObjectMapper) jp.getCodec(); ObjectNode root = (ObjectNode) mapper.readTree(jp); HashMapMessage s = new HashMapMessage(); if (hasProtocolObjectData(root)) { /* * We have a data node with a classId which makes us comfortable * this is a protocol object. So 1) remove the data; 2) find object * class from classId; 3) create object; and 4) put back again... */ JsonNode node = root.remove("data"); int classId = node.get("classId").asInt(); Class<? extends ProtocolObject> cl = factory.create(classId).getClass(); ProtocolObject p = mapper.readValue(node, cl); s.setData(p); } /* * Read the remainder as an ordinary map and put all into the * server messages */ HashMap<String, ?> values = mapper.readValue(root, HashMap.class); s.putAll(values); return s; } // --- PRIVATE METHODS --- // /* * Return true if the object has a data field which has a "classId" field */ private boolean hasProtocolObjectData(ObjectNode root) { JsonNode dataNode = root.get("data"); JsonNode idNode = (dataNode == null ? null : dataNode.get(CLASS_ID_PROPERTY)); return idNode != null; } }); mapper.registerModule(module); }
public <T> void addDeserializer( SimpleModule module, Class<?> type, JsonDeserializer<?> jsonDeserializer) { module.addDeserializer((Class<T>) type, (JsonDeserializer<? extends T>) jsonDeserializer); }