Java Code Examples for com.fasterxml.jackson.core.JsonParser#getBinaryValue()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getBinaryValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BindBean64SharedPreferences.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * for attribute valueByteArray parsing
 */
protected byte[] parseValueByteArray(String input) {
  if (input==null) {
    return null;
  }
  KriptonJsonContext context=KriptonBinder.jsonBind();
  try (JacksonWrapperParser wrapper=context.createParser(input)) {
    JsonParser jacksonParser=wrapper.jacksonParser;
    // START_OBJECT
    jacksonParser.nextToken();
    // value of "element"
    jacksonParser.nextValue();
    byte[] result=null;
    if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
      result=jacksonParser.getBinaryValue();
    }
    return result;
  } catch(Exception e) {
    e.printStackTrace();
    throw(new KriptonRuntimeException(e.getMessage()));
  }
}
 
Example 2
Source File: StoredAsJsonDeserializer.java    From Rosetta with Apache License 2.0 6 votes vote down vote up
@Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
  JavaType javaType = ctxt.getTypeFactory().constructType(type);
  ObjectMapper mapper = (ObjectMapper) jp.getCodec();

  if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
    return deserialize(mapper, jp.getText(), javaType);
  } else if (jp.getCurrentToken() == JsonToken.VALUE_EMBEDDED_OBJECT) {
    String json = new String(jp.getBinaryValue(Base64Variants.getDefaultVariant()), StandardCharsets.UTF_8);
    return deserialize(mapper, json, javaType);
  } else if(jp.getCurrentToken() == JsonToken.START_OBJECT || jp.getCurrentToken() == JsonToken.START_ARRAY) {
    return mapper.readValue(jp, javaType);
  } else {
    throw ctxt.mappingException("Expected JSON String");
  }
}
 
Example 3
Source File: TPMSAttestDeserializer.java    From webauthn4j with Apache License 2.0 6 votes vote down vote up
@Override
public TPMSAttest deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    byte[] value = p.getBinaryValue();
    ByteBuffer buffer = ByteBuffer.wrap(value);
    byte[] magicBytes = new byte[4];
    buffer.get(magicBytes);
    TPMGenerated magic = TPMGenerated.create(magicBytes);
    byte[] typeBytes = new byte[2];
    buffer.get(typeBytes);
    TPMISTAttest type = TPMISTAttest.create(typeBytes);
    int qualifiedSignerSize = UnsignedNumberUtil.getUnsignedShort(buffer);
    byte[] qualifiedSigner = new byte[qualifiedSignerSize];
    buffer.get(qualifiedSigner);
    int extraDataSize = UnsignedNumberUtil.getUnsignedShort(buffer);
    byte[] extraData = new byte[extraDataSize];
    buffer.get(extraData);
    TPMSClockInfo clock = extractClockInfo(buffer);
    BigInteger firmwareVersion = UnsignedNumberUtil.getUnsignedLong(buffer);
    TPMUAttest attested = extractTPMUAttest(type, buffer);
    if (buffer.remaining() > 0) {
        throw new InvalidFormatException(p, "input byte array contains surplus data", value, TPMTPublic.class);
    }

    return new TPMSAttest(magic, type, qualifiedSigner, extraData, clock, firmwareVersion, attested);
}
 
Example 4
Source File: ArrowRecordBatchSerDe.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Override
protected ArrowRecordBatch doDeserialize(JsonParser jparser, DeserializationContext ctxt)
        throws IOException
{
    if (jparser.nextToken() != JsonToken.VALUE_EMBEDDED_OBJECT) {
        throw new IllegalStateException("Expecting " + JsonToken.VALUE_STRING + " but found " + jparser.getCurrentLocation());
    }
    byte[] bytes = jparser.getBinaryValue();
    AtomicReference<ArrowRecordBatch> batch = new AtomicReference<>();
    try {
        return blockAllocator.registerBatch((BufferAllocator root) -> {
            batch.set((ArrowRecordBatch) MessageSerializer.deserializeMessageBatch(
                    new ReadChannel(Channels.newChannel(new ByteArrayInputStream(bytes))), root));
            return batch.get();
        });
    }
    catch (Exception ex) {
        if (batch.get() != null) {
            batch.get().close();
        }
        throw ex;
    }
}
 
Example 5
Source File: BaseDeserializer.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Helper used to extract named byte array fields from the json parser in a streaming fashion.
 *
 * @param jparser The parser to use for extraction.
 * @param expectedFieldName The expected name of the next field in the stream.
 * @return The byte array representation of the requested field.
 * @throws IOException If there is an error parsing the field.
 */
protected byte[] getNextBinaryField(final JsonParser jparser, final String expectedFieldName)
        throws IOException
{
    assertFieldName(jparser, expectedFieldName);

    //move to the value token
    jparser.nextToken();
    return jparser.getBinaryValue();
}
 
Example 6
Source File: TPMTPublicDeserializer.java    From webauthn4j with Apache License 2.0 5 votes vote down vote up
@Override
public TPMTPublic deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    byte[] value = p.getBinaryValue();
    try {
        return deserialize(value);
    } catch (IllegalArgumentException e) {
        throw new InvalidFormatException(p, "input byte array contains surplus data", value, TPMTPublic.class);
    }
}
 
Example 7
Source File: JsonReader.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public byte[] read(JsonParser parser)
    throws IOException, JsonReadException
{
    try {
        // TODO: Jackson's base64 parser is more lenient than we want (it allows whitespace
        // and other junk in some places).  Switch to something more strict.
        byte[] v = parser.getBinaryValue();
        parser.nextToken();
        return v;
    }
    catch (JsonParseException ex) {
        throw JsonReadException.fromJackson(ex);
    }
}
 
Example 8
Source File: PersonBindMap.java    From kripton with Apache License 2.0 5 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Person parseOnJackson(JsonParser jacksonParser) throws Exception {
  Person instance = new Person();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        case "image":
          // field image (mapped with "image")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.image=jacksonParser.getBinaryValue();
          }
        break;
        case "name":
          // field name (mapped with "name")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.name=jacksonParser.getText();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 9
Source File: DataHandlerJsonDeserializer.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public DataHandler deserialize(JsonParser jp, DeserializationContext ctxt)
    throws IOException, JsonProcessingException
{
    final byte[] value = jp.getBinaryValue();
    return new DataHandler(new DataSource() {
        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(value);
        }

        @Override
        public OutputStream getOutputStream() throws IOException {
            throw new IOException();
        }

        @Override
        public String getContentType() {
            return "application/octet-stream";
        }

        @Override
        public String getName() {
            return "json-binary-data";
        }
    });
}
 
Example 10
Source File: BaseCollectionDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.currentToken() == JsonToken.VALUE_STRING ||
        p.currentToken() == JsonToken.VALUE_EMBEDDED_OBJECT) {

        byte[] binaryValue = p.getBinaryValue();
        Intermediate intermediate = createIntermediate();
        intermediate.addAll(binaryValue);
        return finish(intermediate);
    }
    return super.deserialize(p, ctxt);
}
 
Example 11
Source File: BaseCollectionDeserializers.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (p.currentToken() == JsonToken.VALUE_STRING ||
        p.currentToken() == JsonToken.VALUE_EMBEDDED_OBJECT) {

        byte[] binaryValue = p.getBinaryValue();
        Intermediate intermediate = createIntermediate(binaryValue.length);
        intermediate.addAll(binaryValue);
        return finish(intermediate);
    }
    return super.deserialize(p, ctxt);
}
 
Example 12
Source File: AAGUIDDeserializer.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
@Override
public AAGUID deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    return new AAGUID(p.getBinaryValue());
}
 
Example 13
Source File: Bean81SBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Bean81S parseOnJackson(JsonParser jacksonParser) throws Exception {
  Bean81S instance = new Bean81S();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        case "valueByteArray":
          // field valueByteArray (mapped with "valueByteArray")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.valueByteArray=jacksonParser.getBinaryValue();
          }
        break;
        case "valueMapStringInteger":
          // field valueMapStringInteger (mapped with "valueMapStringInteger")
          if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
            HashMap<String, Integer> collection=new HashMap<>();
            String key=null;
            Integer value=null;
            while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
              jacksonParser.nextValue();
              key=jacksonParser.getText();
              jacksonParser.nextValue();
              if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
                value=jacksonParser.getIntValue();
              }
              collection.put(key, value);
              key=null;
              value=null;
              jacksonParser.nextToken();
            }
            instance.valueMapStringInteger=collection;
          }
        break;
        case "valueInteger":
          // field valueInteger (mapped with "valueInteger")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.valueInteger=jacksonParser.getIntValue();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 14
Source File: ByteBeanBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public ByteBean parseOnJackson(JsonParser jacksonParser) throws Exception {
  ByteBean instance = new ByteBean();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        case "value":
          // field value (mapped with "value")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.value=jacksonParser.getBinaryValue();
          }
        break;
        case "value2":
          // field value2 (mapped with "value2")
          if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
            ArrayList<Byte> collection=new ArrayList<>();
            Byte item=null;
            while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
              if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
                item=null;
              } else {
                item=jacksonParser.getByteValue();
              }
              collection.add(item);
            }
            instance.value2=CollectionUtils.asByteArray(collection);
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 15
Source File: StoneSerializers.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public byte [] deserialize(JsonParser p) throws IOException, JsonParseException {
    byte [] value = p.getBinaryValue();
    p.nextToken();
    return value;
}
 
Example 16
Source File: SdkPojoDeserializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
private Object readObject(SdkField<?> field, JsonParser p, DeserializationContext ctxt) throws IOException {

        MarshallingType<?> type = field.marshallingType();
        switch (p.currentToken()) {
            case VALUE_FALSE:
            case VALUE_TRUE: {
                if (type.equals(MarshallingType.BOOLEAN)) {
                    return p.getBooleanValue();
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got boolean field value");
            }

            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT: {
                if (type.equals(MarshallingType.INTEGER)) {
                    return p.getIntValue();
                } else if (type.equals(MarshallingType.LONG)) {
                    return p.getLongValue();
                } else if (type.equals(MarshallingType.FLOAT)) {
                    return p.getFloatValue(); // coerce should work
                } else if (type.equals(MarshallingType.DOUBLE)) {
                    return p.getDoubleValue(); // coerce should work
                } else if (type.equals(MarshallingType.BIG_DECIMAL)) {
                    return p.getDecimalValue(); // coerce should work
                } else if (type.equals(MarshallingType.INSTANT)) { // we serialize as BigDecimals
                    JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(Instant.class));
                    return deser.deserialize(p, ctxt);
                }
                throw new JsonMappingException(p,
                                               "Type mismatch, expecting " + type + " got int/float/double/big_decimal/instant");
            }

            case VALUE_STRING: {
                if (type.equals(MarshallingType.STRING)) {
                    return p.getText();
                } else if (type.equals(MarshallingType.SDK_BYTES)) {
                    byte[] bytes = p.getBinaryValue();
                    return SdkBytes.fromByteArray(bytes);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got string/bytes");
            }

            case START_OBJECT: {
                if (type.equals(MarshallingType.MAP)) {
                    return readMap(field, p, ctxt);
                } else if (type.equals(MarshallingType.SDK_POJO)) {
                    return readPojo(field.constructor().get(), p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got Map/SdkPojo");
            }

            case START_ARRAY: {
                if (type.equals(MarshallingType.LIST)) {
                    return readList(field, p, ctxt);
                }
                throw new JsonMappingException(p, "Type mismatch, expecting " + type + " got List type");
            }

            case VALUE_NULL:
                return null;

            default:
                throw new JsonMappingException(p, "Can not map type " + type + " Token = " + p.currentToken());
        }
    }
 
Example 17
Source File: Bean75BindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Bean75 parseOnJackson(JsonParser jacksonParser) throws Exception {
  Bean75 instance = new Bean75();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "name":
          // field name (mapped with "name")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.name=jacksonParser.getText();
          }
        break;
        case "valueByteArray":
          // field valueByteArray (mapped with "valueByteArray")
          if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
            ArrayList<Byte> collection=new ArrayList<>();
            Byte item=null;
            while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
              if (jacksonParser.currentToken()==JsonToken.VALUE_NULL) {
                item=null;
              } else {
                item=jacksonParser.getByteValue();
              }
              collection.add(item);
            }
            instance.valueByteArray=CollectionUtils.asByteArray(collection);
          }
        break;
        case "valueByteTypeArray":
          // field valueByteTypeArray (mapped with "valueByteTypeArray")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.valueByteTypeArray=jacksonParser.getBinaryValue();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 18
Source File: AttestedCredentialDataDeserializer.java    From webauthn4j with Apache License 2.0 4 votes vote down vote up
@Override
public AttestedCredentialData deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    byte[] value = p.getBinaryValue();
    return attestedCredentialDataConverter.convert(value);
}
 
Example 19
Source File: Bean81TBindMap.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * parse with jackson
 */
@Override
public Bean81T parseOnJackson(JsonParser jacksonParser) throws Exception {
  Bean81T instance = new Bean81T();
  String fieldName;
  if (jacksonParser.currentToken() == null) {
    jacksonParser.nextToken();
  }
  if (jacksonParser.currentToken() != JsonToken.START_OBJECT) {
    jacksonParser.skipChildren();
    return instance;
  }
  while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
    fieldName = jacksonParser.getCurrentName();
    jacksonParser.nextToken();

    // Parse fields:
    switch (fieldName) {
        case "valueInteger":
          // field valueInteger (mapped with "valueInteger")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.valueInteger=jacksonParser.getIntValue();
          }
        break;
        case "id":
          // field id (mapped with "id")
          instance.id=jacksonParser.getLongValue();
        break;
        case "valueMapStringInteger":
          // field valueMapStringInteger (mapped with "valueMapStringInteger")
          if (jacksonParser.currentToken()==JsonToken.START_ARRAY) {
            HashMap<String, Integer> collection=new HashMap<>();
            String key=null;
            Integer value=null;
            while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
              jacksonParser.nextValue();
              key=jacksonParser.getText();
              jacksonParser.nextValue();
              if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
                value=jacksonParser.getIntValue();
              }
              collection.put(key, value);
              key=null;
              value=null;
              jacksonParser.nextToken();
            }
            instance.valueMapStringInteger=collection;
          }
        break;
        case "valueByteArray":
          // field valueByteArray (mapped with "valueByteArray")
          if (jacksonParser.currentToken()!=JsonToken.VALUE_NULL) {
            instance.valueByteArray=jacksonParser.getBinaryValue();
          }
        break;
        default:
          jacksonParser.skipChildren();
        break;}
  }
  return instance;
}
 
Example 20
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
protected byte[] _readBinary(JsonParser p) throws IOException {
    return p.getBinaryValue();
}