Example usage for com.fasterxml.jackson.core JsonFactory JsonFactory

List of usage examples for com.fasterxml.jackson.core JsonFactory JsonFactory

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory JsonFactory.

Prototype

public JsonFactory() 

Source Link

Document

Default constructor used to create factory instances.

Usage

From source file:io.github.cdelmas.spike.restlet.infrastructure.JacksonCustomConverter.java

private ObjectMapper createMapper() {
    JsonFactory jsonFactory = new JsonFactory();
    jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    ObjectMapper mapper = new ObjectMapper(jsonFactory);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new Jdk8Module());
    return mapper;
}

From source file:io.seldon.spark.actions.JobUtils.java

public static ActionData getActionDataFromActionLogLine(String actionLogLine) {
    ActionData actionData = new ActionData();

    String[] parts = actionLogLine.split("\\s+", 3);
    String json = parts[2];//from   w  ww .  java 2  s . c  om
    actionData.timestamp_utc = parts[0];

    JsonFactory jsonF = new JsonFactory();
    try {
        JsonParser jp = jsonF.createParser(json);
        if (jp.nextToken() != JsonToken.START_OBJECT) {
            throw new IOException("Expected data to start with an Object");
        }
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // Let's move to value
            jp.nextToken();
            if (fieldName.equals("client")) {
                actionData.client = jp.getText();
            } else if (fieldName.equals("client_userid")) {
                actionData.client_userid = jp.getText();
            } else if (fieldName.equals("userid")) {
                actionData.userid = jp.getValueAsInt();
            } else if (fieldName.equals("itemid")) {
                actionData.itemid = jp.getValueAsInt();
            } else if (fieldName.equals("client_itemid")) {
                actionData.client_itemid = jp.getText();
            } else if (fieldName.equals("rectag")) {
                actionData.rectag = jp.getText();
            } else if (fieldName.equals("type")) {
                actionData.type = jp.getValueAsInt();
            } else if (fieldName.equals("value")) {
                actionData.value = jp.getValueAsDouble();
            }
        }
        jp.close();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return actionData;
}

From source file:org.gkh.racf.JSONUtil.java

/**
 * Note: Uses Jackson./*from   w w  w . jav a 2  s.co  m*/
 * 
 * @see <a herf=
 *      "http://stackoverflow.com/questions/15791878/reliable-json-string-validator-in-java"
 *      >Reliable JSON String Validator</a>
 * 
 * @param text
 * @return
 */
public static boolean isValidJSONUsingJackson(String text) {
    try {
        JsonFactory factory = new JsonFactory();
        JsonParser parser = factory.createJsonParser(text);
        JsonParser jp = new ObjectMapper().getJsonFactory().createJsonParser(text);
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return true;
}

From source file:com.github.heuermh.ensemblrestclient.JacksonArchivedSequenceConverterTest.java

@Before
public void setUp() {
    jsonFactory = new JsonFactory();
    converter = new JacksonArchivedSequenceConverter(jsonFactory);
}

From source file:com.googlecode.jmxtrans.model.output.SensuWriter2Test.java

@Test
public void metricsAreFormattedCorrectly() throws IOException {
    StringWriter writer = new StringWriter();
    SensuWriter2 sensuWriter = new SensuWriter2(new GraphiteWriter2(ImmutableList.<String>of(), null),
            new JsonFactory());

    sensuWriter.write(writer, ServerFixtures.dummyServer(), QueryFixtures.dummyQuery(),
            ResultFixtures.dummyResults());

    assertThat(writer.toString()).isEqualTo("{\n" + "  \"name\" : \"jmxtrans\",\n"
            + "  \"type\" : \"metric\",\n" + "  \"handler\" : \"graphite\",\n"
            + "  \"output\" : \"host_example_net_4321.MemoryAlias.ObjectPendingFinalizationCount 10 0\\n\"\n"
            + "}");
}

From source file:name.gumartinm.weather.information.parser.JPOSCurrentParser.java

public Current retrieveCurrenFromJPOS(final String jsonData) throws JsonParseException, IOException {
    final JsonFactory f = new JsonFactory();

    final Current currentWeatherData = new Current();
    currentWeatherData.setClouds(new Clouds());
    currentWeatherData.setCoord(new Coord());
    currentWeatherData.setMain(new Main());
    currentWeatherData.setRain(new Rain());
    currentWeatherData.setSys(new Sys());
    currentWeatherData.setSnow(new Snow());
    currentWeatherData.setWeather(new ArrayList<Weather>());
    currentWeatherData.setWind(new Wind());
    final JsonParser jParser = f.createParser(jsonData);

    this.getCurrentWeatherData(currentWeatherData, jParser);

    return currentWeatherData;
}

From source file:org.o3project.ocnrm.odenos.linklayerizer.LinkLayerizerBoundarySet.java

public Map<String, LinklayerizerBoundary> changeJSONBoundariesToBoundaries(String jsonBoundary, String seqNo) {

    logger.info(seqNo + "\t" + "changeJSONBoundariestoBoundaries Start");

    linklayerizerBoundaryMap = new HashMap<String, LinklayerizerBoundary>();
    try {//from  w w w.  j a  v  a 2  s  .  c o m
        JsonFactory factory = new JsonFactory();
        JsonParser jp = factory.createParser(jsonBoundary);
        jp.nextToken();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String mapKey = jp.getCurrentName();
            LinklayerizerBoundary llb = new LinklayerizerBoundary();
            jp.nextToken();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldname = jp.getCurrentName();
                jp.nextToken();
                if ("boundary_id".equals(fieldname)) {
                    llb.setBoundary_id(jp.getText());
                } else if ("lower_nw".equals(fieldname)) {
                    llb.setLower_nw(jp.getText());
                } else if ("lower_nw_node".equals(fieldname)) {
                    llb.setLower_nw_node(jp.getText());
                } else if ("lower_nw_port".equals(fieldname)) {
                    llb.setLower_nw_port(jp.getText());
                } else if ("upper_nw".equals(fieldname)) {
                    llb.setUpper_nw(jp.getText());
                } else if ("upper_nw_node".equals(fieldname)) {
                    llb.setUpper_nw_node(jp.getText());
                } else if ("upper_nw_port".equals(fieldname)) {
                    llb.setUpper_nw_port(jp.getText());
                } else if ("type".equals(fieldname)) {
                    continue;
                } else {
                    throw new IllegalStateException(seqNo + "\t" + "Unrecognized field '" + fieldname + "'!");
                }
            }
            linklayerizerBoundaryMap.put(mapKey, llb);
        }
        jp.close();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    logger.info(seqNo + "\t" + "changeJSONBoundariestoBoundaries End");
    return linklayerizerBoundaryMap;
}

From source file:com.krobothsoftware.commons.parse.ParserJson.java

/**
 * Creates Json Factory. No throwables to worry about.
 * /*from w w w.  ja  v a  2 s .c  o  m*/
 * @since SNC-EXT-JSON 1.0
 */
@Override
public void init() {
    // no errors or exceptions to worry about
    factory = new JsonFactory();
}

From source file:org.springframework.social.wunderlist.api.impl.handler.ErrorResponseWrapper.java

private JsonNode asJson(ClientHttpResponse response) throws IOException {
    ObjectMapper mapper = new ObjectMapper(new JsonFactory());
    return mapper.readValue(response.getBody(), JsonNode.class);
}

From source file:nl.bneijt.javapjson.JavapJsonMojo.java

public void execute() throws MojoExecutionException {
    JsonFactory jsonFactory = new JsonFactory();
    if (!outputDirectory.exists()) {
        throw new MojoExecutionException(
                "No build output directory found. Was looking at \"" + outputDirectory + "\"");
    }//  ww  w .jav  a2s.  co  m

    String jsonDirectory = buildDirectory.getPath() + File.separator + "javap-json";
    File jsonDirectoryFile = new File(jsonDirectory);
    if (!jsonDirectoryFile.exists()) {
        if (!jsonDirectoryFile.mkdir()) {
            throw new MojoExecutionException(
                    "Could not create output directory \"" + jsonDirectoryFile.getPath() + "\"");
        }
        getLog().debug("Created output directory \"" + jsonDirectoryFile.getPath() + "\"");
    }

    for (File classFile : FileUtils.listFiles(outputDirectory, new SuffixFileFilter(CLASS_EXTENSION),
            TrueFileFilter.INSTANCE)) {
        String output = runJavap(classFile);
        JavapLOutput parseL = JavapParser.parseL(output);

        File outputFile = new File(jsonDirectory + File.separator + "current.json");

        try {
            JsonGenerator jsonOutput = jsonFactory.createJsonGenerator(outputFile, JsonEncoding.UTF8);
            parseL.toJsonOnto(jsonOutput);

            //Move file into correct position
            File classDirectory = classFile.getParentFile();
            String classFileName = classFile.getName();
            String restOfDirectory = classDirectory.getPath().substring(outputDirectory.getPath().length());
            File jsonOutputFileDirectory = new File(jsonDirectory + restOfDirectory);
            File jsonOutputFile = new File(jsonOutputFileDirectory.getPath() + File.separator
                    + classFileName.substring(0, classFileName.length() - ".class".length()) + ".json");
            if (!jsonOutputFileDirectory.exists())
                FileUtils.forceMkdir(jsonOutputFileDirectory);
            if (jsonOutputFile.exists()) {
                FileUtils.deleteQuietly(jsonOutputFile);
            }
            FileUtils.moveFile(outputFile, jsonOutputFile);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new MojoExecutionException("Unable to serialize javap output to Json", e);
        }
    }

}