Search in sources :

Example 31 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project OpenAM by OpenRock.

the class RestSTSInstanceConfigTest method testJsonStringMarshalling.

@Test
public void testJsonStringMarshalling() throws IOException {
    RestSTSInstanceConfig origConfig = createInstanceConfig("/bob", WITH_TLS_OFFLOAD_CONFIG, WITH_SAML2_CONFIG, WITH_OIDC_CONFIG, WITH_CUSTOM_VALIDATOR, WITH_CUSTOM_PROVIDER, WITH_CTS_TOKEN_PERSISTENCE);
    /*
        This is how the Crest HttpServletAdapter ultimately constitutes a JsonValue from a json string. See the
        org.forgerock.json.resource.servlet.HttpUtils.parseJsonBody (called from HttpServletAdapter.getJsonContent)
        for details.
         */
    JsonParser parser = new ObjectMapper().getFactory().createParser(origConfig.toJson().toString());
    final Object content = parser.readValueAs(Object.class);
    assertEquals(origConfig, RestSTSInstanceConfig.fromJson(new JsonValue(content)));
}
Also used : JsonValue(org.forgerock.json.JsonValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.testng.annotations.Test)

Example 32 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project OpenAM by OpenRock.

the class SoapSTSInstanceConfigTest method testJsonStringMarshalling.

@Test
public void testJsonStringMarshalling() throws IOException {
    SoapSTSInstanceConfig origConfig = createInstanceConfig("/bobo/instance1", "http://host.com:8080/am", WITH_KEYSTORE_CONFIG, WITH_VALIDATE_CONFIG, DELEGATION_VALIDATORS_SPECIFIED, CUSTOM_DELEGATION_HANDLER, WITH_SAML2_CONFIG, WITH_OIDC_CONFIG, !WITH_CTS_TOKEN_PERSISTENCE);
    /*
        This is how the Crest HttpServletAdapter ultimately constitutes a JsonValue from a json string. See the
        org.forgerock.json.resource.servlet.HttpUtils.parseJsonBody (called from HttpServletAdapter.getJsonContent)
        for details.
         */
    JsonParser parser = new ObjectMapper().getFactory().createParser(origConfig.toJson().toString());
    final Object content = parser.readValueAs(Object.class);
    assertEquals(origConfig, SoapSTSInstanceConfig.fromJson(new JsonValue(content)));
}
Also used : JsonValue(org.forgerock.json.JsonValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.testng.annotations.Test)

Example 33 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.

the class Surrogate223Test method testSurrogatesByteBacked.

// for [core#223]
public void testSurrogatesByteBacked() throws Exception {
    ByteArrayOutputStream out;
    JsonGenerator g;
    final String toQuote = new String(Character.toChars(0x1F602));
    // just sanity check
    assertEquals(2, toQuote.length());
    // default should be disabled:
    //        assertFalse(JSON_F.isEnabled(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES));
    out = new ByteArrayOutputStream();
    g = JSON_F.createGenerator(out);
    g.writeStartArray();
    g.writeString(toQuote);
    g.writeEndArray();
    g.close();
    // brackets, quotes, 4-byte encoding
    assertEquals(2 + 2 + 4, out.size());
    // Also parse back to ensure correctness
    JsonParser p = JSON_F.createParser(out.toByteArray());
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
    // but may revert back to original behavior
    out = new ByteArrayOutputStream();
    g = JSON_F.createGenerator(out);
    //        g.enable(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES);
    g.writeStartArray();
    g.writeString(toQuote);
    g.writeEndArray();
    g.close();
    // brackets, quotes, 2 x 6 byte JSON escape
    assertEquals(2 + 2 + 12, out.size());
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 34 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.

the class Surrogate223Test method testSurrogatesCharBacked.

// for [core#223]
public void testSurrogatesCharBacked() throws Exception {
    Writer out;
    JsonGenerator g;
    final String toQuote = new String(Character.toChars(0x1F602));
    // just sanity check
    assertEquals(2, toQuote.length());
    // default should be disabled:
    //        assertFalse(JSON_F.isEnabled(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES));
    out = new StringWriter();
    g = JSON_F.createGenerator(out);
    g.writeStartArray();
    g.writeString(toQuote);
    g.writeEndArray();
    g.close();
    // brackets, quotes, 2 chars as is
    assertEquals(2 + 2 + 2, out.toString().length());
    // Also parse back to ensure correctness
    JsonParser p = JSON_F.createParser(out.toString());
    assertToken(JsonToken.START_ARRAY, p.nextToken());
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertToken(JsonToken.END_ARRAY, p.nextToken());
    p.close();
    // but may revert back to original behavior
    out = new StringWriter();
    g = JSON_F.createGenerator(out);
    //        g.enable(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES);
    g.writeStartArray();
    g.writeString(toQuote);
    g.writeEndArray();
    g.close();
    // brackets, quotes, 2 x 6 byte JSON escape
    assertEquals(2 + 2 + 12, out.toString().length());
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) StringWriter(java.io.StringWriter) Writer(java.io.Writer) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 35 with JsonParser

use of com.fasterxml.jackson.core.JsonParser in project jackson-core by FasterXML.

the class TestParserOverrides method _testTokenAccess.

/*
    /**********************************************************
    /* Actual test methods
    /**********************************************************
     */
public void _testTokenAccess(JsonFactory jf, boolean useStream) throws Exception {
    final String DOC = "[ ]";
    JsonParser jp = useStream ? jf.createParser(new ByteArrayInputStream(DOC.getBytes("UTF-8"))) : jf.createParser(DOC);
    assertNull(jp.currentToken());
    jp.clearCurrentToken();
    assertNull(jp.currentToken());
    assertNull(jp.getEmbeddedObject());
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.START_ARRAY, jp.currentToken());
    jp.clearCurrentToken();
    assertNull(jp.currentToken());
    // Also: no codec defined by default
    try {
        jp.readValueAsTree();
        fail("Should get exception without codec");
    } catch (IllegalStateException e) {
        verifyException(e, "No ObjectCodec defined");
    }
    jp.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonParser (com.fasterxml.jackson.core.JsonParser)144 IOException (java.io.IOException)43 Test (org.junit.Test)35 JsonFactory (com.fasterxml.jackson.core.JsonFactory)26 StringWriter (java.io.StringWriter)17 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)15 JsonToken (com.fasterxml.jackson.core.JsonToken)14 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)12 SqlNullable (com.facebook.presto.spi.function.SqlNullable)11 SqlType (com.facebook.presto.spi.function.SqlType)11 BaseTest (com.fasterxml.jackson.core.BaseTest)11 UTF8DataInputJsonParser (com.fasterxml.jackson.core.json.UTF8DataInputJsonParser)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonParseException (com.fasterxml.jackson.core.JsonParseException)9 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)7 PrestoException (com.facebook.presto.spi.PrestoException)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)6 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)5