com.fasterxml.jackson.dataformat.cbor.CBORGenerator Java Examples

The following examples show how to use com.fasterxml.jackson.dataformat.cbor.CBORGenerator. 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: AbstractCtapCanonicalCborSerializer.java    From webauthn4j with Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(T value, JsonGenerator gen, SerializerProvider provider) throws IOException {

    List<KeyValue> nonNullValues =
            rules.stream()
                    .map(rule -> {
                        Object fieldValue = rule.getGetter().apply(value);
                        return new KeyValue(rule.getName(), fieldValue);
                    })
                    .filter(item -> item.value != null)
                    .collect(Collectors.toList());
    ((CBORGenerator) gen).writeStartObject(nonNullValues.size()); // This is important to write finite length map

    for (KeyValue nonNullValue : nonNullValues) {
        if (nonNullValue.name instanceof String) {
            gen.writeFieldName((String) nonNullValue.name);
        } else {
            gen.writeFieldId((int) nonNullValue.name);
        }
        gen.writeObject(nonNullValue.value);
    }

    gen.writeEndObject();
}
 
Example #2
Source File: SdkCborGenerator.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Jackson doesn't have native support for timestamp. As per the RFC 7049
 * (https://tools.ietf.org/html/rfc7049#section-2.4.1) we will need to
 * write a tag and write the epoch.
 */
@Override
public StructuredJsonGenerator writeValue(Instant instant) {
    if (!(getGenerator() instanceof CBORGenerator)) {
        throw new IllegalStateException("SdkCborGenerator is not created with a CBORGenerator.");
    }

    CBORGenerator generator = (CBORGenerator) getGenerator();
    try {
        generator.writeTag(CBOR_TAG_TIMESTAMP);
        generator.writeNumber(instant.toEpochMilli());
    } catch (IOException e) {
        throw new JsonGenerationException(e);
    }
    return this;
}
 
Example #3
Source File: SdkCborGenerator.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Jackson doesn't have native support for timestamp. As per the RFC 7049
 * (https://tools.ietf.org/html/rfc7049#section-2.4.1) we will need to
 * write a tag and write the epoch.
 */
@Override
public StructuredJsonGenerator writeValue(Date date) {

    if (!(getGenerator() instanceof CBORGenerator)) {
        throw new IllegalStateException("SdkCborGenerator is not created with a CBORGenerator.");
    }

    CBORGenerator generator = (CBORGenerator) getGenerator();
    try {
        generator.writeTag(CBOR_TAG_TIMESTAP);
        generator.writeNumber(date.getTime());
    } catch (IOException e) {
        throw new JsonGenerationException(e);
    }
    return this;
}
 
Example #4
Source File: CborIllegalValuesTest.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void binaryValue() throws IOException {
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final CBORGenerator generator = new CBORFactory().createGenerator(byteArrayOutputStream);
    generator.writeBinary(new byte[]{(byte) 0x42, (byte) 0x8, (byte) 0x15});
    generator.close();

    final byte[] array = byteArrayOutputStream.toByteArray();
    boolean exceptionThrown = false;
    try {
        CborFactory.readFrom(array);
    } catch (final JsonParseException e) {
        exceptionThrown = true;
        assertThat(e)
                .withFailMessage("offending Object not included in error")
                .hasMessageContaining("420815");
    }
    assertThat(exceptionThrown).isTrue();
}
 
Example #5
Source File: AbstractDDiApiIntegrationTest.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Convert JSON to a CBOR equivalent.
 * 
 * @param json
 *            JSON object to convert
 * @return Equivalent CBOR data
 * @throws IOException
 *             Invalid JSON input
 */
protected static byte[] jsonToCbor(String json) throws IOException {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jsonParser = jsonFactory.createParser(json);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CBORFactory cborFactory = new CBORFactory();
    CBORGenerator cborGenerator = cborFactory.createGenerator(out);
    while (jsonParser.nextToken() != null) {
        cborGenerator.copyCurrentEvent(jsonParser);
    }
    cborGenerator.flush();
    return out.toByteArray();
}
 
Example #6
Source File: ImmutableJsonObject.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static void writeStartObjectWithLength(final SerializationContext serializationContext, int length)
        throws IOException {
    /*
    This is a workaround to ensure that length is encoded in CBOR-Objects.
    A proper API should be available in version 2.11. (2020-02)
    see: https://github.com/FasterXML/jackson-dataformats-binary/issues/3
     */
    final JsonGenerator jacksonGenerator = serializationContext.getJacksonGenerator();
    if (jacksonGenerator instanceof CBORGenerator) {
        CBORGenerator cborGenerator = (CBORGenerator) jacksonGenerator;
        cborGenerator.writeStartObject(length);
    } else {
        jacksonGenerator.writeStartObject();
    }
}