Search in sources :

Example 31 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project geode by apache.

the class PdxToJSON method getJSONByteArray.

public byte[] getJSONByteArray() {
    JsonFactory jf = new JsonFactory();
    HeapDataOutputStream hdos = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator jg = jf.createJsonGenerator(hdos, JsonEncoding.UTF8);
        enableDisableJSONGeneratorFeature(jg);
        getJSONString(jg, m_pdxInstance);
        jg.close();
        return hdos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        hdos.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Example 32 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project winery by eclipse.

the class VisualAppearanceResource method getConnectionTypeForJsPlumbData.

@GET
@ApiOperation(value = "@return JSON object to be used at jsPlumb.registerConnectionType('NAME', <data>)")
@Produces(MediaType.APPLICATION_JSON)
public Response getConnectionTypeForJsPlumbData() {
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter sw = new StringWriter();
    try {
        JsonGenerator jg = jsonFactory.createGenerator(sw);
        jg.writeStartObject();
        jg.writeFieldName("connector");
        jg.writeString("Flowchart");
        jg.writeFieldName("paintStyle");
        jg.writeStartObject();
        jg.writeFieldName("lineWidth");
        jg.writeNumber(this.getLineWidth());
        jg.writeFieldName("strokeStyle");
        jg.writeObject(this.getColor());
        String dash = this.getDash();
        if (!StringUtils.isEmpty(dash)) {
            String dashStyle = null;
            switch(dash) {
                case "dotted":
                    dashStyle = "1 5";
                    break;
                case "dotted2":
                    dashStyle = "3 4";
                    break;
                case "plain":
                    // otherwise, "1 0" can be used
                    break;
            }
            if (dashStyle != null) {
                jg.writeStringField("dashstyle", dashStyle);
            }
        }
        jg.writeEndObject();
        jg.writeFieldName("hoverPaintStyle");
        jg.writeStartObject();
        jg.writeFieldName("strokeStyle");
        jg.writeObject(this.getHoverColor());
        jg.writeEndObject();
        jg.writeStringField("dash", getDash());
        jg.writeStringField("sourceArrowHead", this.getSourceArrowHead());
        jg.writeStringField("targetArrowHead", this.getTargetArrowHead());
        jg.writeStringField("color", this.getColor());
        jg.writeStringField("hoverColor", this.getHoverColor());
        // BEGIN: Overlays
        jg.writeFieldName("overlays");
        jg.writeStartArray();
        // source arrow head
        String head = this.getSourceArrowHead();
        if (!head.equals("none")) {
            jg.writeStartArray();
            jg.writeString(head);
            jg.writeStartObject();
            jg.writeFieldName("location");
            jg.writeNumber(0);
            // arrow should point towards the node and not away from it
            jg.writeFieldName("direction");
            jg.writeNumber(-1);
            jg.writeFieldName("width");
            jg.writeNumber(20);
            jg.writeFieldName("length");
            jg.writeNumber(12);
            jg.writeEndObject();
            jg.writeEndArray();
        }
        // target arrow head
        head = this.getTargetArrowHead();
        if (!head.equals("none")) {
            jg.writeStartArray();
            jg.writeString(head);
            jg.writeStartObject();
            jg.writeFieldName("location");
            jg.writeNumber(1);
            jg.writeFieldName("width");
            jg.writeNumber(20);
            jg.writeFieldName("length");
            jg.writeNumber(12);
            jg.writeEndObject();
            jg.writeEndArray();
        }
        // Type in brackets on the arrow
        jg.writeStartArray();
        jg.writeString("Label");
        jg.writeStartObject();
        jg.writeStringField("id", "label");
        jg.writeStringField("label", "(" + this.res.getName() + ")");
        jg.writeStringField("cssClass", "relationshipTypeLabel");
        jg.writeFieldName("location");
        jg.writeNumber(0.5);
        jg.writeEndObject();
        jg.writeEndArray();
        jg.writeEndArray();
        // END: Overlays
        jg.writeEndObject();
        jg.close();
    } catch (Exception e) {
        VisualAppearanceResource.LOGGER.error(e.getMessage(), e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
    }
    String res = sw.toString();
    return Response.ok(res).build();
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 33 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project atlasmap by atlasmap.

the class JsonModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    Object document = session.getSourceDocument(getDocId());
    // make this a JSON document
    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        JsonParser parser = jsonFactory.createParser(document.toString());
        JsonNode rootNode = objectMapper.readTree(parser);
        ObjectNode parentNode = (ObjectNode) rootNode;
        String parentSegment = "[root node]";
        for (SegmentContext sc : new AtlasPath(field.getPath()).getSegmentContexts(false)) {
            JsonNode currentNode = JsonFieldWriter.getChildNode(parentNode, parentSegment, sc.getSegment());
            if (currentNode == null) {
                return 0;
            }
            if (AtlasPath.isCollectionSegment(sc.getSegment())) {
                if (currentNode != null && currentNode.isArray()) {
                    return currentNode.size();
                }
                return 0;
            }
            parentNode = (ObjectNode) currentNode;
        }
    } catch (IOException e) {
        throw new AtlasException(e.getMessage(), e);
    }
    return 0;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 34 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project Java-Tutorial by gpcodervn.

the class JacksonStreamingWriterExample method main.

public static void main(String[] args) throws IOException {
    File file = new File("data/result.json");
    JsonFactory factory = new JsonFactory();
    /**
     * Write values in JSON format to a file
     */
    JsonGenerator generator = factory.createGenerator(file, JsonEncoding.UTF8);
    // {
    generator.writeStartObject();
    // "name" : "gpcoder"
    generator.writeStringField("name", "GP Coder");
    // "website" : "https://gpcoder.com"
    generator.writeStringField("website", "https://gpcoder.com");
    // "year" : 2017
    generator.writeNumberField("year", 2017);
    // "colors" :
    generator.writeFieldName("posts");
    // [
    generator.writeStartArray();
    // "Java Core"
    generator.writeString("Java Core");
    // "Design Pattern"
    generator.writeString("Design Pattern");
    // "Spring"
    generator.writeString("Spring");
    // ]
    generator.writeEndArray();
    // }
    generator.writeEndObject();
    generator.close();
    System.out.println("Done!");
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) File(java.io.File)

Example 35 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project keycloak by keycloak.

the class ImportUtils method importFederatedUsersFromStream.

// Assuming that it's invoked inside transaction
public static void importFederatedUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
    RealmProvider model = session.realms();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(is);
    try {
        parser.nextToken();
        while (parser.nextToken() == JsonToken.FIELD_NAME) {
            if ("realm".equals(parser.getText())) {
                parser.nextToken();
                String currRealmName = parser.getText();
                if (!currRealmName.equals(realmName)) {
                    throw new IllegalStateException("Trying to import users into invalid realm. Realm name: " + realmName + ", Expected realm name: " + currRealmName);
                }
            } else if ("federatedUsers".equals(parser.getText())) {
                parser.nextToken();
                if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
                    parser.nextToken();
                }
                // TODO: support for more transactions per single users file (if needed)
                List<UserRepresentation> userReps = new ArrayList<UserRepresentation>();
                while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                    UserRepresentation user = parser.readValueAs(UserRepresentation.class);
                    userReps.add(user);
                    parser.nextToken();
                }
                importFederatedUsers(session, model, realmName, userReps);
                if (parser.getCurrentToken() == JsonToken.END_ARRAY) {
                    parser.nextToken();
                }
            }
        }
    } finally {
        parser.close();
    }
}
Also used : RealmProvider(org.keycloak.models.RealmProvider) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) List(java.util.List) JsonParser(com.fasterxml.jackson.core.JsonParser) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8