Example #1
0
  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 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;
  }
Example #3
0
  @Override
  public void onEvalStart() {
    super.onEvalStart();
    initalizeData();
    try {
      ObjectMapper mapper = new ObjectMapper();
      mapper.setDateFormat(new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a"));
      mapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
      mapper.configure(SerializationConfig.Feature.WRAP_EXCEPTIONS, true);
      mapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false);
      mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, true);

      SimpleModule module = new SimpleModule("DataProxy", new Version(1, 0, 0, null));
      module.addSerializer(DataProxy.class, new DataProxySerializer(DataProxy.class));
      mapper.registerModule(module);

      JsonFactory jsonFactory = mapper.getJsonFactory();
      writer = jsonFactory.createJsonGenerator(context.getWriter());
      writer.setPrettyPrinter(new DefaultPrettyPrinter());

      writer.writeStartObject();
    } catch (IOException ex) {
      throw new OseeCoreException(ex);
    }
  }
  @Override
  public ObjectMapper get() {
    final ObjectMapper mapper = new ObjectMapper(jsonFactory);

    // Set the features
    for (Map.Entry<Enum<?>, Boolean> entry : featureMap.entrySet()) {
      final Enum<?> key = entry.getKey();

      if (key instanceof JsonGenerator.Feature) {
        mapper.configure(((JsonGenerator.Feature) key), entry.getValue());
      } else if (key instanceof JsonParser.Feature) {
        mapper.configure(((JsonParser.Feature) key), entry.getValue());
      } else if (key instanceof SerializationConfig.Feature) {
        mapper.configure(((SerializationConfig.Feature) key), entry.getValue());
      } else if (key instanceof DeserializationConfig.Feature) {
        mapper.configure(((DeserializationConfig.Feature) key), entry.getValue());
      } else {
        throw new IllegalArgumentException("Can not configure ObjectMapper with " + key.name());
      }
    }

    for (Module module : modules) {
      mapper.registerModule(module);
    }
    // by default, don't serialize null values.
    mapper.setSerializationInclusion(Inclusion.NON_NULL);

    return mapper;
  }
Example #5
0
  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;
  }
Example #6
0
  @Test
  public void testJackson() throws Exception {

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new VKontakteModule());
    objectMapper.getJsonFactory().setInputDecorator(new VKontakteDecoratorFilter());

    ClassLoader loader = getClass().getClassLoader();
    VKontakteNewsPosts resp =
        objectMapper.readValue(loader.getResource("wall.get.json"), VKontakteNewsPosts.class);
    System.out.println(resp);
  }
Example #7
0
    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);
    }
Example #8
0
  @Test
  public void testYoutubeJackson() throws Exception {

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new YoutubeModule());

    ClassLoader loader = getClass().getClassLoader();
    YoutubeUserProfile profile =
        objectMapper.readValue(
            loader.getResource("youtube.profile.json"), new TypeReference<YoutubeUserProfile>() {});

    Assert.assertEquals(
        profile.getUid(), "http://gdata.youtube.com/feeds/api/users/RjgafKflW0WSEjtinXzprQ");
    Assert.assertEquals(profile.getUsername(), "andhomgmailcom");
  }
  public @Produces @JsonMapper ObjectMapper getJsonMapper(InjectionPoint ip) {

    if (customisedJsonMapper == null) {
      synchronized (JsonMapperFactory.class) {
        if (customisedJsonMapper == null) {

          SimpleModule myModule = new SimpleModule("BRUJUG-TOVO", new Version(1, 0, 0, null));
          myModule.addSerializer(
              new ProposalTypeSerializer()); // serializer declares correct class to bind to

          customisedJsonMapper = new ObjectMapper();
          customisedJsonMapper.registerModule(myModule);
        }
      }
    }

    log.debug("Inject JSON mapper:" + customisedJsonMapper);

    return customisedJsonMapper;
  }
  @Override
  public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper();

    // ignore unknown fields (for backwards compatibility)
    objectMapper.getDeserializationConfig().disable(FAIL_ON_UNKNOWN_PROPERTIES);

    // use ISO dates
    objectMapper.getSerializationConfig().disable(WRITE_DATES_AS_TIMESTAMPS);

    // skip fields that are null instead of writing an explicit json null value
    objectMapper.getSerializationConfig().setSerializationInclusion(NON_NULL);

    // disable auto detection of json properties... all properties must be explicit
    objectMapper
        .getDeserializationConfig()
        .disable(DeserializationConfig.Feature.AUTO_DETECT_FIELDS);
    objectMapper.getDeserializationConfig().disable(AUTO_DETECT_SETTERS);
    objectMapper.getSerializationConfig().disable(SerializationConfig.Feature.AUTO_DETECT_FIELDS);
    objectMapper.getSerializationConfig().disable(AUTO_DETECT_GETTERS);
    objectMapper.getSerializationConfig().disable(AUTO_DETECT_IS_GETTERS);

    if (jsonSerializers != null || jsonDeserializers != null) {
      SimpleModule module = new SimpleModule(getClass().getName(), new Version(1, 0, 0, null));
      if (jsonSerializers != null) {
        for (Entry<Class<?>, JsonSerializer<?>> entry : jsonSerializers.entrySet()) {
          addSerializer(module, entry.getKey(), entry.getValue());
        }
      }
      if (jsonDeserializers != null) {
        for (Entry<Class<?>, JsonDeserializer<?>> entry : jsonDeserializers.entrySet()) {
          addDeserializer(module, entry.getKey(), entry.getValue());
        }
      }
      objectMapper.registerModule(module);
    }

    return objectMapper;
  }