Esempio n. 1
0
 @Override
 public DateMidnight deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // We'll accept either long (timestamp) or array:
   if (jp.isExpectedStartArrayToken()) {
     jp.nextToken(); // VALUE_NUMBER_INT
     int year = jp.getIntValue();
     jp.nextToken(); // VALUE_NUMBER_INT
     int month = jp.getIntValue();
     jp.nextToken(); // VALUE_NUMBER_INT
     int day = jp.getIntValue();
     if (jp.nextToken() != JsonToken.END_ARRAY) {
       throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after DateMidnight ints");
     }
     return new DateMidnight(year, month, day);
   }
   switch (jp.getCurrentToken()) {
     case VALUE_NUMBER_INT:
       return new DateMidnight(jp.getLongValue());
     case VALUE_STRING:
       DateTime local = parseLocal(jp);
       if (local == null) {
         return null;
       }
       return local.toDateMidnight();
   }
   throw ctxt.wrongTokenException(
       jp, JsonToken.START_ARRAY, "expected JSON Array, Number or String");
 }
Esempio n. 2
0
  public Object deserialize(InputStream is, Hashtable extendedContext) throws Exception {

    // getting deserializer
    Class targetCls = (Class) extendedContext.get("targetClass");
    Object objectOfTargetCls = targetCls.newInstance();
    TypeDesc desc =
        (TypeDesc)
            objectOfTargetCls
                .getClass()
                .getMethod("getTypeDesc", new Class[] {})
                .invoke(objectOfTargetCls, new Object[] {});
    final QName xmlType; // = desc.getXmlType();
    xmlType =
        new QName(
            "http://" + objectOfTargetCls.getClass().getName(),
            org.uengine.util.UEngineUtil.getClassNameOnly(objectOfTargetCls.getClass()));
    Deserializer dser =
        (Deserializer)
            objectOfTargetCls
                .getClass()
                .getMethod("getDeserializer", new Class[] {String.class, Class.class, QName.class})
                .invoke(
                    objectOfTargetCls, new Object[] {"", objectOfTargetCls.getClass(), xmlType});
    // end
    System.out.println("dser:" + dser);

    DeserializationContext context =
        new DeserializationContextImpl(
            new org.xml.sax.InputSource(is),
            /*			new MessageContext(null){
            	public String getEncodingStyle(){
            		return xmlType.getNamespaceURI();
            	}
            },*/
            new MessageContext(new AxisClient()),
            // Message.RESPONSE
            "PurchaseOrder");

    boolean oldVal = context.isDoneParsing();
    ((DeserializationContextImpl) context).deserializing(true);
    context.pushElementHandler(new EnvelopeHandler((SOAPHandler) dser));

    //	        context.getRecorder().replay(0, -1, (org.xml.sax.ContentHandler)context);
    context.getRecorder().replay(0, -1, (org.xml.sax.ContentHandler) context);

    ((DeserializationContextImpl) context).deserializing(oldVal);

    context.parse();

    return dser.getValue();
  }
  /** This is the trickiest thing to handle, since property we are looking for may be anywhere... */
  @Override
  public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext ctxt)
      throws IOException, JsonProcessingException {
    // but first, sanity check to ensure we have START_OBJECT or FIELD_NAME
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.START_OBJECT) {
      t = jp.nextToken();
    } else if (t != JsonToken.FIELD_NAME) {
      throw ctxt.wrongTokenException(
          jp,
          JsonToken.START_OBJECT,
          "need JSON Object to contain As.PROPERTY type information (for class "
              + baseTypeName()
              + ")");
    }
    // Ok, let's try to find the property. But first, need token buffer...
    TokenBuffer tb = null;

    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
      String name = jp.getCurrentName();
      jp.nextToken(); // to point to the value
      if (_propertyName.equals(name)) { // gotcha!
        JsonDeserializer<Object> deser = _findDeserializer(ctxt, jp.getText());
        // deserializer should take care of closing END_OBJECT as well
        if (tb != null) {
          jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
        }
        /* Must point to the next value; tb had no current, jp
         * pointed to VALUE_STRING:
         */
        jp.nextToken(); // to skip past String value
        // deserializer should take care of closing END_OBJECT as well
        return deser.deserialize(jp, ctxt);
      }
      if (tb == null) {
        tb = new TokenBuffer(null);
      }
      tb.writeFieldName(name);
      tb.copyCurrentStructure(jp);
    }
    // Error if we get here...
    throw ctxt.wrongTokenException(
        jp,
        JsonToken.FIELD_NAME,
        "missing property '"
            + _propertyName
            + "' that is to contain type id  (for class "
            + baseTypeName()
            + ")");
  }
  public Object deserialize(JsonParser jp, DeserializationContext ctxt)
      throws IOException, JsonProcessingException {
    // Ok: must point to START_ARRAY
    if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
      /* 04-Oct-2009, tatu: One exception; byte arrays are generally
       *   serialized as base64, so that should be handled
       */
      if (jp.getCurrentToken() == JsonToken.VALUE_STRING && _elementClass == Byte.class) {
        return deserializeFromBase64(jp, ctxt);
      }
      throw ctxt.mappingException(_arrayType.getRawClass());
    }

    final ObjectBuffer buffer = ctxt.leaseObjectBuffer();
    Object[] chunk = buffer.resetAndStart();
    int ix = 0;
    JsonToken t;
    final TypeDeserializer typeDeser = _elementTypeDeserializer;

    while ((t = jp.nextToken()) != JsonToken.END_ARRAY) {
      // Note: must handle null explicitly here; value deserializers won't
      Object value;

      if (t == JsonToken.VALUE_NULL) {
        value = null;
      } else if (typeDeser == null) {
        value = _elementDeserializer.deserialize(jp, ctxt);
      } else {
        value = _elementDeserializer.deserializeWithType(jp, ctxt, typeDeser);
      }
      if (ix >= chunk.length) {
        chunk = buffer.appendCompletedChunk(chunk);
        ix = 0;
      }
      chunk[ix++] = value;
    }

    Object[] result;

    if (_untyped) {
      result = buffer.completeAndClearBuffer(chunk, ix);
    } else {
      result = buffer.completeAndClearBuffer(chunk, ix, _elementClass);
    }
    ctxt.returnObjectBuffer(buffer);
    return result;
  }
 protected Byte[] deserializeFromBase64(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // First same as what ArrayDeserializers.ByteDeser does:
   byte[] b = jp.getBinaryValue(ctxt.getBase64Variant());
   // But then need to convert to wrappers
   Byte[] result = new Byte[b.length];
   for (int i = 0, len = b.length; i < len; ++i) {
     result[i] = Byte.valueOf(b[i]);
   }
   return result;
 }
Esempio n. 6
0
    @Override
    public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
      // We'll accept either long (timestamp) or array:
      if (jp.isExpectedStartArrayToken()) {
        jp.nextToken(); // VALUE_NUMBER_INT
        int year = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int month = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int day = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int hour = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int minute = jp.getIntValue();
        jp.nextToken(); // VALUE_NUMBER_INT
        int second = jp.getIntValue();
        // let's leave milliseconds optional?
        int millisecond = 0;
        if (jp.nextToken() != JsonToken.END_ARRAY) { // VALUE_NUMBER_INT
          millisecond = jp.getIntValue();
          jp.nextToken(); // END_ARRAY?
        }
        if (jp.getCurrentToken() != JsonToken.END_ARRAY) {
          throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after LocalDateTime ints");
        }
        return new LocalDateTime(year, month, day, hour, minute, second, millisecond);
      }

      switch (jp.getCurrentToken()) {
        case VALUE_NUMBER_INT:
          return new LocalDateTime(jp.getLongValue());
        case VALUE_STRING:
          DateTime local = parseLocal(jp);
          if (local == null) {
            return null;
          }
          return local.toLocalDateTime();
      }
      throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Array or Number");
    }
 @Override
 public Map<Object, Object> deserialize(
     JsonParser jp, DeserializationContext ctxt, Map<Object, Object> result)
     throws IOException, JsonProcessingException {
   // Ok: must point to START_OBJECT or FIELD_NAME
   JsonToken t = jp.getCurrentToken();
   if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME) {
     throw ctxt.mappingException(getMapClass());
   }
   _readAndBind(jp, ctxt, result);
   return result;
 }
Esempio n. 8
0
 @Override
 public Boolean read(DeserializationContext context) throws IOException {
   byte b = context.readByte();
   switch (b) {
     case 0:
       return Boolean.FALSE;
     case 1:
       return Boolean.TRUE;
     default:
       throw new DeserializingException("Unexpected boolean flag: " + b);
   }
 }
Esempio n. 9
0
 @Override
 public ReadablePeriod deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // TODO: perhaps support array of numbers...
   // if (jp.isExpectedStartArrayToken()) { ]
   switch (jp.getCurrentToken()) {
     case VALUE_NUMBER_INT: // assume it's millisecond count
       return new Period(jp.getLongValue());
     case VALUE_STRING:
       return new Period(jp.getText());
   }
   throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Number or String");
 }
Esempio n. 10
0
 protected Object _unwrapAndDeserialize(
     JsonParser jp, DeserializationContext ctxt, JavaType rootType, JsonDeserializer<Object> deser)
     throws IOException, JsonParseException, JsonMappingException {
   SerializedString rootName = _provider.findExpectedRootName(ctxt.getConfig(), rootType);
   if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
     throw JsonMappingException.from(
         jp,
         "Current token not START_OBJECT (needed to unwrap root name '"
             + rootName
             + "'), but "
             + jp.getCurrentToken());
   }
   if (jp.nextToken() != JsonToken.FIELD_NAME) {
     throw JsonMappingException.from(
         jp,
         "Current token not FIELD_NAME (to contain expected root name '"
             + rootName
             + "'), but "
             + jp.getCurrentToken());
   }
   String actualName = jp.getCurrentName();
   if (!rootName.getValue().equals(actualName)) {
     throw JsonMappingException.from(
         jp,
         "Root name '"
             + actualName
             + "' does not match expected ('"
             + rootName
             + "') for type "
             + rootType);
   }
   // ok, then move to value itself....
   jp.nextToken();
   Object result;
   if (_valueToUpdate == null) {
     result = deser.deserialize(jp, ctxt);
   } else {
     deser.deserialize(jp, ctxt, _valueToUpdate);
     result = _valueToUpdate;
   }
   // and last, verify that we now get matching END_OBJECT
   if (jp.nextToken() != JsonToken.END_OBJECT) {
     throw JsonMappingException.from(
         jp,
         "Current token not END_OBJECT (to match wrapper object with root name '"
             + rootName
             + "'), but "
             + jp.getCurrentToken());
   }
   return result;
 }
 @Override
 public Map<Object, Object> deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   // Ok: must point to START_OBJECT, or FIELD_NAME
   JsonToken t = jp.getCurrentToken();
   if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME) {
     throw ctxt.mappingException(getMapClass());
   }
   if (_propertyBasedCreator != null) {
     return _deserializeUsingCreator(jp, ctxt);
   }
   Map<Object, Object> result;
   if (_defaultCtor == null) {
     throw ctxt.instantiationException(getMapClass(), "No default constructor found");
   }
   try {
     result = _defaultCtor.newInstance();
   } catch (Exception e) {
     throw ctxt.instantiationException(getMapClass(), e);
   }
   _readAndBind(jp, ctxt, result);
   return result;
 }
Esempio n. 12
0
 @SuppressWarnings("unchecked")
 @Override
 public T deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   JsonToken t = jp.getCurrentToken();
   if (t == JsonToken.VALUE_NUMBER_INT) {
     return (T) new DateTime(jp.getLongValue(), DateTimeZone.UTC);
   }
   if (t == JsonToken.VALUE_STRING) {
     String str = jp.getText().trim();
     if (str.length() == 0) { // [JACKSON-360]
       return null;
     }
     return (T) new DateTime(str, DateTimeZone.UTC);
   }
   throw ctxt.mappingException(getValueClass());
 }
Esempio n. 13
0
 @Override
 public List read(DeserializationContext context) throws IOException {
   return context.readList(Object.class);
 }
Esempio n. 14
0
 @Override
 public Map read(DeserializationContext context) throws IOException {
   return context.readMap(Object.class, Object.class);
 }
Esempio n. 15
0
 @Override
 public Set read(DeserializationContext context) throws IOException {
   return new HashSet<>(context.readList(Object.class));
 }
Esempio n. 16
0
 @Override
 public String read(DeserializationContext context) throws IOException {
   return context.readString();
 }
 @Override
 public AbstractLocation read(DeserializationContext context) throws IOException {
   return doRead(context.in());
 }
Esempio n. 18
0
 @Override
 public UUID read(DeserializationContext context) throws IOException {
   return new UUID(context.readLong(), context.readLong());
 }
Esempio n. 19
0
 @Override
 public Byte read(DeserializationContext context) throws IOException {
   return context.readByte();
 }
Esempio n. 20
0
 @Override
 public Short read(DeserializationContext context) throws IOException {
   return context.readShort();
 }
Esempio n. 21
0
 @Override
 public Character read(DeserializationContext context) throws IOException {
   return context.readChar();
 }
Esempio n. 22
0
 @Override
 public Integer read(DeserializationContext context) throws IOException {
   return context.readInt();
 }