Exemplo n.º 1
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);
    }
  }
Exemplo n.º 2
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;
  }
Exemplo n.º 3
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);
    }
Exemplo n.º 4
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");
    }
  }
  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;
  }
 private <T> void addSerializer(
     SimpleModule module, Class<?> type, JsonSerializer<?> jsonSerializer) {
   module.addSerializer((Class<? extends T>) type, (JsonSerializer<T>) jsonSerializer);
 }
 public <T> void addDeserializer(
     SimpleModule module, Class<?> type, JsonDeserializer<?> jsonDeserializer) {
   module.addDeserializer((Class<T>) type, (JsonDeserializer<? extends T>) jsonDeserializer);
 }
  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;
  }
Exemplo n.º 9
0
 @Override
 public void setupModule(SetupContext context) {
   addSerializer(Node.class, new NodeSerializer(typeContext));
   addDeserializer(Node.class, new NodeDeserializer(typeContext));
   super.setupModule(context);
 }
Exemplo n.º 10
0
  public MdmiMapper() {
    super();
    // SimpleModule startInfoSerializerModule = new SimpleModule("Start", new Version(1, 0, 0,
    // null));

    SimpleModule nodeSerializerModule = new SimpleModule("Node", new Version(1, 0, 0, null));
    nodeSerializerModule.addSerializer(
        org.openhealthtools.mdht.mdmi.model.Node.class,
        new JsonSerializer<org.openhealthtools.mdht.mdmi.model.Node>() {

          @Override
          public void serialize(
              org.openhealthtools.mdht.mdmi.model.Node node,
              JsonGenerator jgen,
              SerializerProvider arg2)
              throws IOException, JsonProcessingException {
            jgen.writeStartObject();
            // jgen.writeObject(node.get);
            jgen.writeObjectField("FieldName", node.getFieldName());
            jgen.writeObjectField("Location", node.getLocation());
            jgen.writeObjectField("Name", node.getName());
            // jgen.writeObjectField("TargetBER", ci.trgBER != null ? ci.trgBER : "null");
            jgen.writeEndObject();
          }
        });
    registerModule(nodeSerializerModule);

    SimpleModule toMessageElementSerializerModule =
        new SimpleModule("ToMessageElement", new Version(1, 0, 0, null));
    toMessageElementSerializerModule.addSerializer(
        ToMessageElement.class,
        new JsonSerializer<ToMessageElement>() {

          @Override
          public void serialize(
              ToMessageElement toMessageElement, JsonGenerator jgen, SerializerProvider arg2)
              throws IOException, JsonProcessingException {
            jgen.writeStartObject();
            jgen.writeObjectField("toString", toMessageElement.toString());
            // jgen.writeObjectField("TargetBER", ci.trgBER != null ? ci.trgBER : "null");
            jgen.writeEndObject();
          }
        });
    registerModule(toMessageElementSerializerModule);

    SimpleModule toBusinessElementSerializerModule =
        new SimpleModule("ToBusinessElement", new Version(1, 0, 0, null));
    toBusinessElementSerializerModule.addSerializer(
        ToBusinessElement.class,
        new JsonSerializer<ToBusinessElement>() {

          @Override
          public void serialize(
              ToBusinessElement toBusinessElement, JsonGenerator jgen, SerializerProvider arg2)
              throws IOException, JsonProcessingException {
            jgen.writeStartObject();

            jgen.writeObjectField("toString", toBusinessElement.toString());
            // jgen.writeObjectField("TargetBER", ci.trgBER != null ? ci.trgBER : "null");
            jgen.writeEndObject();
          }
        });
    registerModule(toBusinessElementSerializerModule);

    SimpleModule xValueSerializerModule = new SimpleModule("XValue", new Version(1, 0, 0, null));
    xValueSerializerModule.addSerializer(
        XValue.class,
        new JsonSerializer<XValue>() {

          @Override
          public void serialize(XValue xValue, JsonGenerator jgen, SerializerProvider arg2)
              throws IOException, JsonProcessingException {
            jgen.writeStartObject();
            if (xValue.getValue() != null) {
              jgen.writeObjectField("value", xValue.getValue());
            }

            if (xValue.getValues() != null) {
              jgen.writeObjectField("values", xValue.getValues());
            }

            // jgen.writeObjectField("SourceBER", ci.srcBER != null ? ci.srcBER : "null");
            // jgen.writeObjectField("TargetBER", ci.trgBER != null ? ci.trgBER : "null");
            jgen.writeEndObject();
          }
        });
    registerModule(xValueSerializerModule);

    SimpleModule conversionInfoSerializerModule =
        new SimpleModule("ConversionInfo", new Version(1, 0, 0, null));
    conversionInfoSerializerModule.addSerializer(
        ConversionInfo.class,
        new JsonSerializer<ConversionInfo>() {

          @Override
          public void serialize(ConversionInfo ci, JsonGenerator jgen, SerializerProvider arg2)
              throws IOException, JsonProcessingException {
            jgen.writeStartObject();
            jgen.writeObjectField("SourceBER", ci.srcBER != null ? ci.srcBER : "null");
            jgen.writeObjectField("TargetBER", ci.trgBER != null ? ci.trgBER : "null");
            jgen.writeObjectField("SemanticElement", ci.target != null ? ci.target : "null");

            jgen.writeEndObject();
          }
        });
    registerModule(conversionInfoSerializerModule);

    SimpleModule mdmiBusinessElementReferenceSerializerModule =
        new SimpleModule("MdmiBusinessElementReference", new Version(1, 0, 0, null));
    mdmiBusinessElementReferenceSerializerModule.addSerializer(
        MdmiBusinessElementReference.class,
        new JsonSerializer<MdmiBusinessElementReference>() {

          @Override
          public void serialize(
              MdmiBusinessElementReference mber, JsonGenerator jgen, SerializerProvider arg2)
              throws IOException, JsonProcessingException {
            jgen.writeStartObject();
            jgen.writeObjectField("name", mber.getName());
            jgen.writeObjectField(
                "parent", (mber.getParent() != null ? mber.getParent().getName() : ""));
            // jgen.writeObjectField("name", mber.get);
            jgen.writeEndObject();
          }
        });
    registerModule(mdmiBusinessElementReferenceSerializerModule);

    SimpleModule dateTimeSerializerModule =
        new SimpleModule("DateTimeSerializerModule", new Version(1, 0, 0, null));
    dateTimeSerializerModule.addSerializer(ElementValueSet.class, new ElementValueSetSerializer());
    registerModule(dateTimeSerializerModule);

    SimpleModule xElementValueSerializerModule =
        new SimpleModule("XElementValueSerializer", new Version(1, 0, 0, null));
    xElementValueSerializerModule.addSerializer(XElementValue.class, new XElementValueSerializer());
    registerModule(xElementValueSerializerModule);

    SimpleModule semanticElementSerializerModule =
        new SimpleModule("SemanticElementSerializer", new Version(1, 0, 0, null));
    xElementValueSerializerModule.addSerializer(
        SemanticElement.class, new SemanticElementSerializer());
    registerModule(semanticElementSerializerModule);

    SimpleModule xDataStructSerializerrModule =
        new SimpleModule("XDataStructSerializer", new Version(1, 0, 0, null));
    xDataStructSerializerrModule.addSerializer(XDataStruct.class, new XDataStructSerializer());
    registerModule(xDataStructSerializerrModule);
  }