javatips.net counter

Java Examples for org.codehaus.jackson.JsonFactory

The following java examples will help you to understand the usage of org.codehaus.jackson.JsonFactory. These source code samples are taken from different open source projects.

Example 1
Project: fast-http-master  File: TestRelationshipSerializationStrategy.java View source code
@Test
public void shouldSerializeRelationshipWithNoProperties() {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    JsonSerializer serializer = new JsonSerializer(new JsonFactory(), buffer);
    Node fromNode = mock(Node.class);
    when(fromNode.getId()).thenReturn(0l);
    Node toNode = mock(Node.class);
    when(toNode.getId()).thenReturn(1l);
    Relationship mockRel = mock(Relationship.class);
    when(mockRel.getId()).thenReturn(0l);
    when(mockRel.getType()).thenReturn(DynamicRelationshipType.withName("LOVES"));
    when(mockRel.getStartNode()).thenReturn(fromNode);
    when(mockRel.getEndNode()).thenReturn(toNode);
    when(mockRel.getPropertyKeys()).thenReturn(Collections.<String>emptyList());
    RelationshipSerializationStrategy strategy = new RelationshipSerializationStrategy();
    strategy.serialize(mockRel, serializer);
    serializer.flush();
    assertThat(new String(buffer.toByteArray()), is("{" + "\"self\":\"/db/data/relationship/0\"," + "\"type\":\"LOVES\"," + "\"start\":\"/db/data/node/0\"," + "\"end\":\"/db/data/node/1\"," + "\"data\":{}," + "\"extensions\":{}" + "}"));
}
Example 2
Project: bintray-client-java-master  File: ObjectMapperHelper.java View source code
private static ObjectMapper buildDetailsMapper() {
    ObjectMapper mapper = new ObjectMapper(new JsonFactory());
    // TODO: when moving to Jackson 2.x these can be replaced with JodaModule
    mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
    //Don't include keys with null values implicitly, only explicitly set values should be sent over
    mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, false);
    mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
    mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
    return mapper;
}
Example 3
Project: cs-244-project-master  File: PBFTServerInstance.java View source code
private GroupConfigProvider<PBFTCohort.Client> initializeConfigProvider(File file) throws NoSuchMethodException, IOException, FileNotFoundException {
    BufferedReader reader = new BufferedReader(new FileReader(file));
    JsonFactory factory = new JsonFactory();
    JsonParser jsonParser = factory.createJsonParser(reader);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(jsonParser);
    JsonNode servers = root.get("servers");
    GroupMember<PBFTCohort.Client> leader = null;
    Set<GroupMember<PBFTCohort.Client>> clients = Sets.newHashSet();
    int leaderId = root.get("primary").getIntValue();
    Iterator<JsonNode> elements = servers.getElements();
    GroupMember<PBFTCohort.Client> me = null;
    while (elements.hasNext()) {
        JsonNode server = elements.next();
        GroupMember<PBFTCohort.Client> client = serverNodeToClient(server);
        if (client.getReplicaID() == leaderId) {
            leader = client;
        }
        if (client.getReplicaID() == replicaID) {
            me = client;
        }
        clients.add(client);
    }
    return new StaticGroupConfigProvider<PBFTCohort.Client>(leader, me, clients, INITIAL_VIEW_ID);
}
Example 4
Project: jersey-old-master  File: Stax2JacksonWriterTest.java View source code
private void _testBean(Class clazz, Object bean) throws Exception {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(JAXBContextImpl.RETAIN_REFERENCE_TO_INFO, Boolean.TRUE);
    Class[] classes = new Class[] { clazz };
    JAXBContext ctx = JAXBContext.newInstance(classes, props);
    JsonFactory factory = new JsonFactory();
    Writer osWriter = new OutputStreamWriter(System.out);
    JsonGenerator g;
    g = factory.createJsonGenerator(osWriter);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.marshal(bean, new Stax2JacksonWriter(g));
    g.flush();
    System.out.println("");
}
Example 5
Project: ovirt-engine-master  File: BrandingManagerTest.java View source code
@Test
public void testGetMessages() throws JsonParseException, IOException {
    String result = testManager.getMessages("user_portal", Locale.US);
    //$NON-NLS-1$
    assertNotNull("There should be a result", result);
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser parser = factory.createJsonParser(result);
    JsonNode resultNode = mapper.readTree(parser);
    // There should be 5 key value pairs (1 from user portal, 4 common)
    //$NON-NLS-1$
    assertEquals("Size should be 5", 5, resultNode.size());
    //$NON-NLS-1$ //$NON-NLS-2$
    assertEquals("User Portal", resultNode.get("application_title").getTextValue());
}
Example 6
Project: spring-social-instagram-master  File: InstagramErrorHandler.java View source code
@SuppressWarnings("unchecked")
private Map<String, Object> extractErrorDetailsFromResponse(ClientHttpResponse response) throws IOException {
    ObjectMapper mapper = new ObjectMapper(new JsonFactory());
    Map<String, Object> responseMap = mapper.<Map<String, Object>>readValue(response.getBody(), new TypeReference<Map<String, Object>>() {
    });
    if (responseMap.containsKey("meta")) {
        Map<String, Object> meta = (Map<String, Object>) responseMap.get("meta");
        if (Integer.valueOf(String.valueOf(meta.get("code"))).intValue() > 200) {
            return meta;
        }
    }
    return null;
}
Example 7
Project: stickypunch-master  File: TestUtils.java View source code
public static String getRandomLogJson() {
    JsonFactory jsonfactory = new JsonFactory();
    Writer writer = new StringWriter();
    String json = null;
    try {
        JsonGenerator jsonGenerator = jsonfactory.createJsonGenerator(writer);
        jsonGenerator.writeStartObject();
        jsonGenerator.writeArrayFieldStart("logs");
        jsonGenerator.writeString("authenticationToken must be at least 16 characters.");
        jsonGenerator.writeString("Downloading push notification package failed.");
        jsonGenerator.writeString("Extracting push notification package failed.");
        jsonGenerator.writeString("Missing file in push notification package.");
        jsonGenerator.writeString("Missing image in push notification package.");
        jsonGenerator.writeEndArray();
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
        json = writer.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return json;
}
Example 8
Project: AIM-master  File: MeasureOverheadServlet.java View source code
@Override
public void doService(Request req, Response resp) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
    String json = "";
    if (br != null) {
        json = br.readLine();
    }
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);
    String probeClassName = json;
    LOGGER.info("Requested overhead measurement for probe {}", probeClassName);
    OverheadData oData = new OverheadData();
    List<OverheadRecord> records = OverheadEstimator.measureOverhead(probeClassName);
    oData.setoRecords(records);
    resp.setContentType("application/json");
    mapper.writeValue(resp.getOutputStream(), oData);
}
Example 9
Project: Anicetus-master  File: JsonEncoder.java View source code
/**
	 * Encode the telemetry graph as JSON.
	 * 
	 * @param session
	 *          The telemetry graph to encode.
	 * @return the JSON string as a character array.
	 */
public char[] encode(GlobalInfo session) {
    CharArrayWriter out = new CharArrayWriter();
    try {
        JsonGenerator gen = (new JsonFactory()).createJsonGenerator(out);
        writeEntity(session, gen, true);
        gen.close();
        out.close();
    } catch (IOException ioe) {
        m_exceptionHandler.exceptionCaught(ioe);
    }
    return out.toCharArray();
}
Example 10
Project: contrib-jenkins-qubell-plugin-master  File: JsonParser.java View source code
/**
     * Parses a key value pair map from simple json expression
     * @param value string representation of json object
     * @return parsed value or null for invalid input
     */
public static Map<String, Object> parseMap(String value) {
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper(factory);
    TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
    };
    HashMap<String, Object> parsedMap;
    try {
        parsedMap = mapper.readValue(value, typeRef);
        return parsedMap;
    } catch (IOException e) {
        return null;
    }
}
Example 11
Project: dk-master  File: JsonUtil.java View source code
/**
	 * json 对象转字符串
	 * @param obj
	 * @return
	 * @throws IOException
	 */
public static String toString(Object obj) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    // mapper.getSerializationConfig().setDateFormat(new
    // SimpleDateFormat("yyyy-MM-dd"));
    StringWriter sw = new StringWriter();
    JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
    mapper.writeValue(gen, obj);
    gen.close();
    return sw.toString();
}
Example 12
Project: fluxtream-app-master  File: GoogleLatitudeUpdater.java View source code
private int parseLocations(final ApiKey apiKey, final InputStream inputStream) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory jfactory = mapper.getJsonFactory();
    JsonParser jParser = jfactory.createJsonParser(inputStream);
    JsonToken token;
    List<LocationFacet> locations = new ArrayList<LocationFacet>();
    getToLocationData(jParser);
    int nLocations = 0;
    while ((token = jParser.nextToken()) != null) {
        if (token == JsonToken.START_OBJECT) {
            jParser.nextToken();
            parseLocation(apiKey, jParser, locations);
            if (locations.size() == 1000) {
                nLocations += 1000;
                boolean connectorIsLive = persistLocations(apiKey, locations);
                if (!connectorIsLive)
                    break;
                locations.clear();
            }
        }
    }
    nLocations += locations.size();
    if (apiKey != null)
        persistLocations(apiKey, locations);
    return nLocations;
}
Example 13
Project: Growth-and-Decay-master  File: Util.java View source code
public static Object getObjFromJson(byte[] json) {
    // for thread safety, we make our own.
    JsonFactory jsonFactory = new JsonFactory();
    try {
        JsonParser jp = jsonFactory.createJsonParser(json);
        JsonResourceParser jrp = new JsonResourceParser(jp);
        return jrp.parse();
    } catch (Exception e) {
        OpenFeintInternal.log(TAG, e.getMessage());
    }
    return null;
}
Example 14
Project: ISIS_Android_Viewer-master  File: ROClient.java View source code
public <T extends JsonRepr> T executeT(Class<T> t, String httpMethod, RORequest roRequest, Map<String, Object> args) throws com.dimuthuupeksha.viewer.android.applib.exceptions.JsonParseException, ConnectionException, InvalidCredentialException, UnknownErrorException {
    HttpResponse response = execute(httpMethod, roRequest, args);
    try {
        String json = EntityUtils.toString(response.getEntity());
        JsonParser jp = new JsonFactory().createJsonParser(json);
        ObjectMapper objectMapper = new ObjectMapper();
        T representation = objectMapper.readValue(jp, t);
        return representation;
    } catch (Exception e) {
        e.printStackTrace();
        throw new com.dimuthuupeksha.viewer.android.applib.exceptions.JsonParseException();
    }
}
Example 15
Project: nuxeo-master  File: TestDocumentModelReader.java View source code
@Test
public void IGetTheSameDocAsVcs() throws Exception {
    // I create a document
    DocumentModel doc = session.createDocumentModel("/", "somefile", "File");
    doc.setPropertyValue("dc:title", "Some file");
    session.createDocument(doc);
    session.save();
    TransactionHelper.commitOrRollbackTransaction();
    TransactionHelper.startTransaction();
    WorkManager wm = Framework.getLocalService(WorkManager.class);
    Assert.assertTrue(wm.awaitCompletion(20, TimeUnit.SECONDS));
    esa.refresh();
    // search and retrieve from ES
    ElasticSearchService ess = Framework.getLocalService(ElasticSearchService.class);
    DocumentModelList docs = ess.query(new NxQueryBuilder(session).nxql("SELECT * FROM File").fetchFromElasticsearch());
    Assert.assertEquals(1, docs.totalSize());
    DocumentModel esDoc = docs.get(0);
    // esDoc.detach(false);
    Assert.assertNotNull(esDoc);
    // search from ES retrieve with VCS
    docs = ess.query(new NxQueryBuilder(session).nxql("SELECT * FROM File").fetchFromDatabase());
    DocumentModel vcsDoc = docs.get(0);
    // compare both docs
    Assert.assertNotNull(esDoc);
    Assert.assertEquals(vcsDoc, esDoc);
    JsonFactory factory = new JsonFactory();
    OutputStream out = new ByteArrayOutputStream();
    JsonGenerator jsonGen = factory.createJsonGenerator(out);
    new JsonESDocumentWriter().writeESDocument(jsonGen, esDoc, null, null);
    String esJson = out.toString();
    out = new ByteArrayOutputStream();
    jsonGen = factory.createJsonGenerator(out);
    new JsonESDocumentWriter().writeESDocument(jsonGen, vcsDoc, null, null);
    String vcsJson = out.toString();
    Assert.assertEquals(vcsJson, esJson);
}
Example 16
Project: org.handwerkszeug.riak-master  File: ProtoBufMapReduceResponse.java View source code
@Override
public ArrayNode getResponse() {
    if (this.resp.hasResponse()) {
        ByteString bs = this.resp.getResponse();
        JsonFactory factory = new JsonFactory(new ObjectMapper());
        try {
            JsonParser parser = factory.createJsonParser(bs.newInput());
            JsonNode jn = parser.readValueAsTree();
            if (jn instanceof ArrayNode) {
                ArrayNode an = (ArrayNode) jn;
                return an;
            } else {
                throw new IllegalStateException(String.format(Messages.MapReduceResponseMustBeArray, jn));
            }
        } catch (IOException e) {
            throw new RiakException(e);
        }
    }
    return null;
}
Example 17
Project: parroteer-master  File: SocketDataSenderTest.java View source code
@Before
public void setUp() {
    when(navData.getState()).thenReturn(navDataState);
    when(socketClient.isConnected()).thenReturn(true);
    ObjectMapper objectMapper = new ObjectMapper();
    JsonFactory jsonFactory = new JsonFactory();
    socketDataSender = new SocketDataSender(socketClient, jsonFactory, objectMapper);
}
Example 18
Project: private_project_iparty-master  File: ShareList.java View source code
public static ShareList parse(InputStream stream) throws AppException {
    ShareList shareList = new ShareList();
    ObjectMapper om = new ObjectMapper();
    try {
        JsonNode rootNode = om.readTree(stream);
        int result = rootNode.path("result").getIntValue();
        if (result == 0) {
            // 失败
            String why = rootNode.path("description").getTextValue();
            L.i("��失败:" + why);
            throw AppException.fail(why);
        } else if (result == 1) {
            // �功
            JsonNode dataNode = rootNode.path("details");
            JsonFactory f = new JsonFactory();
            JsonParser jp = f.createJsonParser(dataNode.toString());
            jp.nextToken();
            while (jp.nextToken() == JsonToken.START_OBJECT) {
                IshareInfo info = om.readValue(jp, IshareInfo.class);
                shareList.getShareList().add(info);
            }
            return shareList;
        } else {
            throw AppException.fail("接�异常");
        }
    } catch (JsonProcessingException e) {
        L.i("json转�失败");
        throw AppException.json(e);
    } catch (IOException e) {
        throw AppException.io(e);
    }
}
Example 19
Project: rest-client-master  File: JSONUtil.java View source code
public static String indentJSON(final String jsonIn) throws JSONParseException {
    JsonFactory fac = new JsonFactory();
    try {
        JsonParser parser = fac.createJsonParser(new StringReader(jsonIn));
        JsonNode node = null;
        try {
            node = jsonObjMapper.readTree(parser);
        } catch (JsonParseException ex) {
            throw new JSONParseException(ex.getMessage());
        }
        StringWriter out = new StringWriter();
        // Create pretty printer:
        JsonGenerator gen = fac.createJsonGenerator(out);
        DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
        pp.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());
        gen.setPrettyPrinter(pp);
        // Now write:
        jsonObjMapper.writeTree(gen, node);
        gen.flush();
        gen.close();
        return out.toString();
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }
    return jsonIn;
}
Example 20
Project: WebServices-master  File: JSONUtil.java View source code
public static String indentJSON(final String jsonIn) throws JSONParseException {
    JsonFactory fac = new JsonFactory();
    try {
        JsonParser parser = fac.createJsonParser(new StringReader(jsonIn));
        JsonNode node = null;
        try {
            node = jsonObjMapper.readTree(parser);
        } catch (JsonParseException ex) {
            throw new JSONParseException(ex.getMessage());
        }
        StringWriter out = new StringWriter();
        // Create pretty printer:
        JsonGenerator gen = fac.createJsonGenerator(out);
        DefaultPrettyPrinter pp = new DefaultPrettyPrinter();
        pp.indentArraysWith(new DefaultPrettyPrinter.Lf2SpacesIndenter());
        gen.setPrettyPrinter(pp);
        // Now write:
        jsonObjMapper.writeTree(gen, node);
        gen.flush();
        gen.close();
        return out.toString();
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, null, ex);
    }
    return jsonIn;
}
Example 21
Project: activityinfo-master  File: AdminLevelResource.java View source code
@GET
@Path("/entities/features")
public Response getFeatures(@InjectParam EntityManager em) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(baos, Charsets.UTF_8);
    List<AdminEntity> entities = em.createQuery("select e  from AdminEntity e where e.deleted = false and e.level = :level").setParameter("level", level).getResultList();
    JsonFactory jfactory = new JsonFactory();
    JsonGenerator json = jfactory.createJsonGenerator(writer);
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    json.setPrettyPrinter(prettyPrinter);
    json.writeStartObject();
    json.writeStringField("type", "FeatureCollection");
    json.writeFieldName("features");
    json.writeStartArray();
    GeometrySerializer geometrySerializer = new GeometrySerializer();
    for (AdminEntity entity : entities) {
        if (entity.getGeometry() != null) {
            json.writeStartObject();
            json.writeStringField("type", "Feature");
            json.writeNumberField("id", entity.getId());
            json.writeObjectFieldStart("properties");
            json.writeStringField("name", entity.getName());
            if (entity.getParentId() != null) {
                json.writeNumberField("parentId", entity.getParentId());
            }
            json.writeEndObject();
            json.writeFieldName("geometry");
            geometrySerializer.writeGeometry(json, entity.getGeometry());
            json.writeEndObject();
        }
    }
    json.writeEndArray();
    json.writeEndObject();
    json.close();
    return Response.ok().entity(baos.toByteArray()).type(MediaType.APPLICATION_JSON).build();
}
Example 22
Project: Android-Templates-And-Utilities-master  File: ExampleParser.java View source code
public static Response<List<ProductEntity>> parse(InputStream stream) throws IOException, JsonParseException {
    Response<List<ProductEntity>> response = null;
    // init parser
    JsonFactory factory = new JsonFactory();
    JsonParser parser = null;
    parser = factory.createJsonParser(stream);
    // parse JSON
    if (parser.nextToken() == JsonToken.START_OBJECT)
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            // error
            if (parser.getCurrentName().equals("error")) {
                String type = null;
                String message = null;
                if (parser.nextToken() == JsonToken.START_OBJECT)
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        if (parser.getCurrentName().equals("type")) {
                            if (parser.getCurrentToken() == JsonToken.VALUE_STRING)
                                type = parser.getText();
                        } else if (parser.getCurrentName().equals("message")) {
                            if (parser.getCurrentToken() == JsonToken.VALUE_STRING)
                                message = parser.getText();
                        }
                    }
                response = new Response<>();
                response.setError(true);
                response.setErrorType(type);
                response.setErrorMessage(message);
            } else // response
            if (parser.getCurrentName().equals("product")) {
                long id = -1L;
                String name = null;
                if (parser.nextToken() == JsonToken.START_OBJECT)
                    while (parser.nextToken() != JsonToken.END_OBJECT) {
                        if (parser.getCurrentName().equals("id")) {
                            if (parser.getCurrentToken() == JsonToken.VALUE_NUMBER_INT)
                                id = parser.getLongValue();
                        } else if (parser.getCurrentName().equals("name")) {
                            if (parser.getCurrentToken() == JsonToken.VALUE_STRING)
                                name = parser.getText();
                        } else {
                            // unknown parameter
                            handleUnknownParameter(parser);
                        }
                    }
                ProductEntity product = new ProductEntity();
                product.setId(id);
                product.setName(name);
                List<ProductEntity> productList = new ArrayList<>();
                productList.add(product);
                response = new Response<>();
                response.setResponseObject(productList);
            }
        }
    // close parser
    if (parser != null)
        parser.close();
    return response;
}
Example 23
Project: databus-master  File: FieldToAvro.java View source code
public String buildAvroSchema(String namespace, String topRecordAvroName, String topRecordDatabaseName, String[][] headers, TableTypeInfo topRecordTypeInfo) {
    if (namespace == null)
        throw new IllegalArgumentException("namespace should not be null.");
    if (topRecordAvroName == null)
        throw new IllegalArgumentException("topRecordAvroName should not be null.");
    if (topRecordDatabaseName == null)
        throw new IllegalArgumentException("topRecordDatabaseName should not be null.");
    if (topRecordTypeInfo == null)
        throw new IllegalArgumentException("topRecordTypeInfo should not be null.");
    FieldInfo fieldInfo = new FieldInfo(topRecordDatabaseName, topRecordTypeInfo, -1);
    Map<String, Object> field = fieldToAvro(fieldInfo, true);
    // Overwrite the name with the nice Java record name
    field.put("name", topRecordAvroName);
    // Add namespace
    field.put("namespace", namespace);
    // Add doc and serialize to JSON
    try {
        SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a zzz");
        field.put("doc", "Auto-generated Avro schema for " + topRecordDatabaseName + ". Generated at " + df.format(new Date(System.currentTimeMillis())));
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = new JsonFactory();
        StringWriter writer = new StringWriter();
        JsonGenerator jgen = factory.createJsonGenerator(writer);
        jgen.useDefaultPrettyPrinter();
        mapper.writeValue(jgen, field);
        return writer.getBuffer().toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Example 24
Project: distributed_loadgen-master  File: Config.java View source code
public void setConfig(String configjson) {
    JsonFactory factory = new JsonFactory();
    try {
        JsonParser p = factory.createJsonParser(configjson.getBytes());
        p.nextToken();
        p.nextToken();
        if (p.getCurrentName().equals(PROPERTIES_JSON_ID)) {
            p.nextToken();
            while (p.nextToken() != JsonToken.END_OBJECT) {
                String key = p.getCurrentName();
                JsonToken token = p.nextToken();
                if (token == JsonToken.VALUE_NUMBER_INT && properties.get(key) instanceof Integer) {
                    properties.put(key, new Integer(p.getIntValue()));
                } else if (token == JsonToken.VALUE_NUMBER_FLOAT && properties.get(key) instanceof Double) {
                    properties.put(key, new Double(p.getDoubleValue()));
                } else if ((token == JsonToken.VALUE_FALSE || token == JsonToken.VALUE_TRUE) && properties.get(key) instanceof Boolean) {
                    properties.put(key, new Boolean(p.getBooleanValue()));
                } else if (token == JsonToken.VALUE_STRING && properties.get(key) instanceof String) {
                    properties.put(key, p.getText());
                } else {
                    LOG.error("Field for " + key + " did not match type");
                }
            }
        } else {
            LOG.error("Attempted to set an invalid config");
        }
    } catch (JsonParseException e) {
        LOG.error("Attempted to set an invalid config");
    } catch (IOException e) {
        LOG.error("Attempted to set an invalid config");
    }
}
Example 25
Project: HMS-master  File: JAXBUtil.java View source code
public static String print(RestSource x) throws IOException {
    try {
        JsonFactory jf = new JsonFactory();
        StringWriter sw = new StringWriter();
        JsonGenerator jp = jf.createJsonGenerator(sw);
        jp.useDefaultPrettyPrinter();
        mapper.writeValue(jp, x);
        jp.close();
        String buffer = sw.toString();
        return buffer;
    } catch (Throwable e) {
        throw new IOException(e);
    }
}
Example 26
Project: incubator-blur-master  File: GlobalJsonAuthorization.java View source code
@Override
public void setupProvider(BlurConfiguration config) throws Exception {
    String securityFile = config.get("blur.console.authorization.provider.globaljson.file");
    if (securityFile != null) {
        JsonFactory factory = new JsonFactory();
        ObjectMapper mapper = new ObjectMapper(factory);
        File from = new File(securityFile);
        TypeReference<Map<String, Map<String, String>>> typeRef = new TypeReference<Map<String, Map<String, String>>>() {
        };
        try {
            Map<String, Map<String, String>> properties = mapper.readValue(from, typeRef);
            globalUserProperties = Collections.unmodifiableMap(properties);
        } catch (Exception e) {
            log.error("Unable to parse security file.  Search may not work right.", e);
            globalUserProperties = null;
        }
    }
}
Example 27
Project: jstorm-master  File: JSONUtil.java View source code
/**
	 * 将map转化为json
	 * 
	 * @param map
	 * @return
	 */
public String formatMap2JSON(Map<String, Object> map) {
    StringWriter stringWriter = new StringWriter();
    String json = "";
    try {
        JsonGenerator gen = new JsonFactory().createJsonGenerator(stringWriter);
        MAPPER.writeValue(gen, map);
        gen.close();
        json = stringWriter.toString();
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    return json;
}
Example 28
Project: nifi-master  File: SiteToSiteReceiver.java View source code
public TransactionCompletion receiveFiles() throws IOException {
    Transaction transaction = siteToSiteClient.createTransaction(TransferDirection.RECEIVE);
    JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(output);
    jsonGenerator.writeStartArray();
    DataPacket dataPacket;
    while ((dataPacket = transaction.receive()) != null) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("attributes");
        jsonGenerator.writeStartObject();
        Map<String, String> attributes = dataPacket.getAttributes();
        if (attributes != null) {
            for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
                jsonGenerator.writeStringField(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        jsonGenerator.writeEndObject();
        InputStream data = dataPacket.getData();
        if (data != null) {
            jsonGenerator.writeBinaryField("data", IOUtils.toByteArray(data));
        }
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
    jsonGenerator.close();
    transaction.confirm();
    return transaction.complete();
}
Example 29
Project: nuxeo-mule-connector-master  File: TypeDefinitionFecther.java View source code
public JsonNode fetch() {
    InputStream jsonStream = null;
    // fallback to local static definition
    try {
        jsonStream = this.getClass().getResource("/default-def/types.json").openStream();
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = new JsonFactory();
        JsonParser parser = factory.createJsonParser(jsonStream);
        return mapper.readTree(parser);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
Example 30
Project: palava-ipc-json-master  File: JsonTest.java View source code
/**
     * Tests application boot.
     * 
     * @since 1.0
     * @throws IOException should not happen
     */
@Test
public void boot() throws IOException {
    final Framework framework = Palava.newFramework();
    framework.start();
    final Client client = new NettyClient();
    final ClientConnection connection = client.connect("localhost", 8081);
    Writer writer;
    JsonGenerator generator;
    writer = new StringWriter();
    generator = new JsonFactory().createJsonGenerator(writer);
    generator.writeStartObject();
    generator.writeFieldName("name");
    generator.writeString(getClass().getName());
    generator.writeEndObject();
    generator.flush();
    generator.close();
    Assert.assertEquals(writer.toString(), connection.send(writer.toString()));
    writer = new StringWriter();
    generator = new JsonFactory().createJsonGenerator(writer);
    generator.writeStartArray();
    generator.writeString(getClass().getName());
    generator.writeEndArray();
    generator.flush();
    generator.close();
    Assert.assertEquals(writer.toString(), connection.send(writer.toString()));
    framework.stop();
}
Example 31
Project: sleuthkit-hadoop-master  File: GrepCountReducer.java View source code
@Override
protected void setup(Context context) throws IOException {
    // TODO: We could make sure that we only have one instance of this
    // here, since that's all I'm supporting at the moment.
    regexes = context.getConfiguration().get("mapred.mapper.regex").split("\n");
    gen = new JsonFactory().createJsonGenerator(buf, JsonEncoding.UTF8);
    gen.writeStartArray();
}
Example 32
Project: teiid-designer-master  File: RestUtils.java View source code
private static List<String> parseRepositories(final String jsonResponse) throws Exception {
    if (!jsonResponse.isEmpty()) {
        final JsonFactory jsonFactory = new JsonFactory();
        final JsonParser jp = jsonFactory.createJsonParser(jsonResponse);
        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode rootNode = mapper.readTree(jp);
        // should only get one repositories node back
        if ((rootNode != null) && (rootNode.size() == 1)) {
            final JsonNode repositoriesNode = rootNode.iterator().next();
            final List<String> result = new ArrayList<String>(repositoriesNode.size());
            for (final JsonNode repoNode : repositoriesNode) {
                //$NON-NLS-1$
                result.add(repoNode.path("name").getTextValue());
            }
            return result;
        }
    }
    return Collections.emptyList();
}
Example 33
Project: monitoring-master  File: ObjectMapper_1_x_IT.java View source code
@Test
public void testConstructor() throws Exception {
    ObjectMapper mapper1 = new ObjectMapper();
    ObjectMapper mapper2 = new ObjectMapper(new JsonFactory());
    Constructor<?> omConstructor1 = ObjectMapper.class.getConstructor();
    Constructor<?> omConstructor2 = ObjectMapper.class.getConstructor(JsonFactory.class);
    Constructor<?> omConstructor3 = ObjectMapper.class.getConstructor(JsonFactory.class, SerializerProvider.class, DeserializerProvider.class);
    Class<?> serializationConfig = null;
    Class<?> deserializationConfig = null;
    try {
        serializationConfig = Class.forName("org.codehaus.jackson.map.SerializationConfig");
        deserializationConfig = Class.forName("org.codehaus.jackson.map.DeserializationConfig");
    } catch (ClassNotFoundException ignored) {
    }
    Constructor<?> omConstructor4 = null;
    if (serializationConfig != null && deserializationConfig != null) {
        omConstructor4 = ObjectMapper.class.getConstructor(JsonFactory.class, SerializerProvider.class, DeserializerProvider.class, serializationConfig, deserializationConfig);
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    if (omConstructor4 != null) {
        verifier.verifyTrace(event(SERVICE_TYPE, omConstructor4));
    }
    verifier.verifyTrace(event(SERVICE_TYPE, omConstructor3), event(SERVICE_TYPE, omConstructor1));
    if (omConstructor4 != null) {
        verifier.verifyTrace(event(SERVICE_TYPE, omConstructor4));
    }
    verifier.verifyTrace(event(SERVICE_TYPE, omConstructor3), event(SERVICE_TYPE, omConstructor2));
    verifier.verifyTraceCount(0);
}
Example 34
Project: pinpoint-master  File: ObjectMapper_1_x_IT.java View source code
@Test
public void testConstructor() throws Exception {
    ObjectMapper mapper1 = new ObjectMapper();
    ObjectMapper mapper2 = new ObjectMapper(new JsonFactory());
    Constructor<?> omConstructor1 = ObjectMapper.class.getConstructor();
    Constructor<?> omConstructor2 = ObjectMapper.class.getConstructor(JsonFactory.class);
    Constructor<?> omConstructor3 = ObjectMapper.class.getConstructor(JsonFactory.class, SerializerProvider.class, DeserializerProvider.class);
    Class<?> serializationConfig = null;
    Class<?> deserializationConfig = null;
    try {
        serializationConfig = Class.forName("org.codehaus.jackson.map.SerializationConfig");
        deserializationConfig = Class.forName("org.codehaus.jackson.map.DeserializationConfig");
    } catch (ClassNotFoundException ignored) {
    }
    Constructor<?> omConstructor4 = null;
    if (serializationConfig != null && deserializationConfig != null) {
        omConstructor4 = ObjectMapper.class.getConstructor(JsonFactory.class, SerializerProvider.class, DeserializerProvider.class, serializationConfig, deserializationConfig);
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
    if (omConstructor4 != null) {
        verifier.verifyTrace(event(SERVICE_TYPE, omConstructor4));
    }
    verifier.verifyTrace(event(SERVICE_TYPE, omConstructor3), event(SERVICE_TYPE, omConstructor1));
    if (omConstructor4 != null) {
        verifier.verifyTrace(event(SERVICE_TYPE, omConstructor4));
    }
    verifier.verifyTrace(event(SERVICE_TYPE, omConstructor3), event(SERVICE_TYPE, omConstructor2));
    verifier.verifyTraceCount(0);
}
Example 35
Project: archived-net-virt-platform-master  File: LogMessageDocWikiText.java View source code
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    String inputFile = System.getProperty("org.sdnplatform.doc." + "LogMessageDocWikiText.inputFile");
    String outputFile = System.getProperty("org.sdnplatform.doc." + "LogMessageDocWikiText.outputFile");
    InputStream is = new FileInputStream(inputFile);
    OutputStream os = new FileOutputStream(outputFile);
    PrintWriter out = new PrintWriter(os);
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory f = new JsonFactory();
    JsonParser jp = f.createJsonParser(is);
    MappingIterator<LogMessageDocItemData> mi = mapper.readValues(jp, LogMessageDocItemData.class);
    Map<String, List<LogMessageDocItemData>> lmdiMap = new HashMap<String, List<LogMessageDocItemData>>();
    while (mi.hasNext()) {
        LogMessageDocItemData lmdi = mi.nextValue();
        List<LogMessageDocItemData> dlist = lmdiMap.get(lmdi.getCategory());
        if (dlist == null)
            lmdiMap.put(lmdi.getCategory(), dlist = new ArrayList<LogMessageDocItemData>());
        dlist.add(lmdi);
    }
    List<String> categories = new ArrayList<String>();
    categories.addAll(lmdiMap.keySet());
    Collections.sort(categories);
    for (String category : categories) {
        List<LogMessageDocItemData> dlist = lmdiMap.get(category);
        Collections.sort(dlist);
        out.println("h2. " + category);
        for (LogMessageDocItemData lmdi : dlist) {
            out.println("*Process Name*: sdnplatform");
            out.println("*Class Name*: _" + wikiEscape(lmdi.getClassName()) + "_");
            out.println("*Message*: {{" + wikiEscape(lmdi.getMessage()) + "}}");
            out.println("*Severity*: " + wikiEscape(lmdi.getSeverity()));
            out.println("*Explanation*: " + wikiEscape(lmdi.getExplanation()));
            out.println("*Recommendation*: " + wikiEscape(lmdi.getRecommendation()));
            out.println("");
        }
        out.println("");
    }
    out.close();
    os.close();
}
Example 36
Project: artificer-master  File: AbstractUploadServlet.java View source code
/**
	 * Writes the response values back to the http response.  This allows the calling code to
	 * parse the response values for display to the user.
	 *
	 * @param responseMap the response params to write to the http response
	 * @param response the http response
	 * @throws IOException
	 */
protected static void writeToResponse(Map<String, String> responseMap, HttpServletResponse response) throws IOException {
    // Note: setting the content-type to text/html because otherwise IE prompt the user to download
    // the result rather than handing it off to the GWT form response handler.
    // See JIRA issue https://issues.jboss.org/browse/SRAMPUI-103
    response.setContentType("text/html; charset=UTF8");
    JsonFactory f = new JsonFactory();
    JsonGenerator g = f.createJsonGenerator(response.getOutputStream(), JsonEncoding.UTF8);
    g.useDefaultPrettyPrinter();
    g.writeStartObject();
    for (java.util.Map.Entry<String, String> entry : responseMap.entrySet()) {
        String key = entry.getKey();
        String val = entry.getValue();
        g.writeStringField(key, val);
    }
    g.writeEndObject();
    g.flush();
    g.close();
}
Example 37
Project: bamboo-artifactory-plugin-master  File: PromotionThread.java View source code
/**
     * Checks the status and return true on success
     *
     * @param response
     * @param dryRun
     * @return
     */
private boolean checkSuccess(HttpResponse response, boolean dryRun) throws IOException {
    StatusLine status = response.getStatusLine();
    String content = entityToString(response);
    if (status.getStatusCode() != 200) {
        if (dryRun) {
            String message = "Promotion failed during dry run (no change in Artifactory was done): " + status + "<br/>" + content;
            releaseLog.logMessage(message);
        } else {
            String message = "Promotion failed. View Artifactory logs for more details: " + status + "<br/>" + content;
            releaseLog.logError(message);
        }
        return false;
    }
    JsonFactory factory = createJsonFactory();
    JsonParser parser = factory.createJsonParser(content);
    JsonNode root = parser.readValueAsTree();
    JsonNode messagesNode = root.get("messages");
    for (JsonNode node : messagesNode) {
        String level = node.get("level").getTextValue();
        String message = node.get("message").getTextValue();
        if (("WARNING".equals(level) || "ERROR".equals(level)) && !message.startsWith("No items were")) {
            String errorMessage = "Received " + level + ": " + message;
            releaseLog.logError(errorMessage);
            return false;
        }
    }
    return true;
}
Example 38
Project: gobblin-master  File: FieldAttributeBasedDeltaFieldsProvider.java View source code
private ObjectNode getDeltaPropValue(String json) {
    try {
        JsonFactory jf = new JsonFactory();
        JsonParser jp = jf.createJsonParser(json);
        ObjectMapper objMap = new ObjectMapper(jf);
        jp.setCodec(objMap);
        return (ObjectNode) jp.readValueAsTree();
    } catch (IOException e) {
        return null;
    }
}
Example 39
Project: grefine-rdf-extension-master  File: RdfSchemaSerializationTest.java View source code
void testJsonEquivalence(String actual, String expected) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getJsonFactory();
    JsonParser jp = factory.createJsonParser(actual);
    JsonNode actualObj = mapper.readTree(jp);
    jp = factory.createJsonParser(expected);
    JsonNode expectedObj = mapper.readTree(jp);
    assertEquals(actualObj, expectedObj);
}
Example 40
Project: gwt-test-utils-master  File: JsonUtilsPatcher.java View source code
private static JsonFactory getFactory() {
    if (factory == null) {
        factory = new JsonFactory();
        factory.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        factory.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        factory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        factory.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
        factory.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        factory.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        factory.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
    }
    return factory;
}
Example 41
Project: hiho-master  File: TestGenericDBLoadDataMapper.java View source code
@Test
public final void testSetUp() throws Exception {
    Mapper.Context context = mock(Mapper.Context.class);
    Configuration conf = mock(Configuration.class);
    GenericDBLoadDataMapper mapper = new GenericDBLoadDataMapper();
    mapper.setDelimiter(",");
    //Configuration conf = new Configuration();
    when(context.getConfiguration()).thenReturn(conf);
    when(context.getConfiguration().get(HIHOConf.INPUT_OUTPUT_DELIMITER)).thenReturn(",");
    ArrayList<ColumnInfo> tableInfo = new ArrayList<ColumnInfo>();
    ColumnInfo columnInfo1 = new ColumnInfo();
    columnInfo1.setIndex(0);
    columnInfo1.setName("id");
    columnInfo1.setType(Types.BIGINT);
    ColumnInfo columnInfo2 = new ColumnInfo();
    columnInfo2.setIndex(1);
    columnInfo2.setName("name");
    columnInfo2.setType(Types.VARCHAR);
    ColumnInfo columnInfo3 = new ColumnInfo();
    columnInfo3.setIndex(2);
    columnInfo3.setName("isValid");
    columnInfo3.setType(Types.BOOLEAN);
    /*ColumnInfo columnInfo4 = new ColumnInfo();
		columnInfo4.setIndex(3);
		columnInfo4.setName("date");
		columnInfo4.setType(Types.DATE);*/
    ColumnInfo columnInfo5 = new ColumnInfo();
    columnInfo5.setIndex(4);
    columnInfo5.setName("percent");
    columnInfo5.setType(Types.DOUBLE);
    tableInfo.add(columnInfo1);
    tableInfo.add(columnInfo2);
    tableInfo.add(columnInfo3);
    //tableInfo.add(columnInfo4);
    tableInfo.add(columnInfo5);
    ObjectMapper objectMapper = new ObjectMapper();
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter writer = new StringWriter();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
    objectMapper.writeValue(jsonGenerator, tableInfo);
    String jsonString = writer.toString();
    when(context.getConfiguration().get(HIHOConf.COLUMN_INFO)).thenReturn(jsonString);
    mapper.setup(context);
    assertEquals(mapper.getTableInfo().toString(), tableInfo.toString());
}
Example 42
Project: iow-hadoop-streaming-master  File: ParquetAsJsonInputFormat.java View source code
@Override
protected List<String> groupToStrings(SimpleGroup grp) {
    JsonGenerator generator;
    CharArrayWriter jWriter;
    List<String> jls = new ArrayList<>();
    jWriter = new CharArrayWriter();
    JsonFactory jFactory = new JsonFactory();
    try {
        generator = jFactory.createJsonGenerator(jWriter);
        groupToJson(generator, grp);
        generator.close();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    jls.add(jWriter.toString());
    return jls;
}
Example 43
Project: jersey-1.x-old-master  File: Stax2JacksonWriterTest.java View source code
private void _testBean(Class clazz, Object bean) throws Exception {
    Map<String, Object> props = JSONHelper.createPropertiesForJaxbContext(Collections.<String, Object>emptyMap());
    Class[] classes = new Class[] { clazz };
    JAXBContext ctx = JAXBContext.newInstance(classes, props);
    JsonFactory factory = new JsonFactory();
    Writer osWriter = new OutputStreamWriter(System.out);
    JsonGenerator g;
    g = factory.createJsonGenerator(osWriter);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.marshal(bean, new Stax2JacksonWriter(g, clazz, ctx));
    g.flush();
    System.out.println("");
}
Example 44
Project: jst-master  File: JSONUtil.java View source code
/**
	 * 将map转化为json
	 * 
	 * @param map
	 * @return
	 */
public String formatMap2JSON(Map<String, Object> map) {
    StringWriter stringWriter = new StringWriter();
    String json = "";
    try {
        JsonGenerator gen = new JsonFactory().createJsonGenerator(stringWriter);
        MAPPER.writeValue(gen, map);
        gen.close();
        json = stringWriter.toString();
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    return json;
}
Example 45
Project: koubei-mongo-master  File: MongoUtil.java View source code
/**
	 * 
	 * @param object
	 * @return
	 * @throws IOException
	 */
public static String objectToJson(Object object) throws IOException {
    String json = null;
    ObjectMapper mapper = MongoMapper.getInstance();
    StringWriter writer = new StringWriter();
    JsonGenerator generator = new JsonFactory().createJsonGenerator(writer);
    mapper.writeValue(generator, object);
    generator.close();
    json = writer.toString();
    writer.close();
    return json;
}
Example 46
Project: moonshine-master  File: FireBugAppender.java View source code
@Override
protected void append(ILoggingEvent event) {
    if (response.get() == null) {
        return;
    }
    event.prepareForDeferredProcessing();
    String message = layout.doLayout(event);
    String filename = null;
    Integer line = null;
    if (event.hasCallerData()) {
        StackTraceElement callerData = event.getCallerData()[0];
        filename = callerData.getFileName();
        line = callerData.getLineNumber();
    }
    StringWriter writer = new StringWriter();
    try {
        JsonGenerator builder = new JsonFactory().createJsonGenerator(writer);
        builder.writeStartArray();
        builder.writeStartObject();
        builder.writeFieldName("Type");
        builder.writeString(event.getLevel().toString());
        builder.writeFieldName("File");
        if (filename != null) {
            builder.writeString(filename);
        } else {
            builder.writeNull();
        }
        builder.writeFieldName("Line");
        if (line != null) {
            builder.writeNumber(line);
        } else {
            builder.writeNull();
        }
        builder.writeFieldName("Label");
        builder.writeString("[S]");
        builder.writeEndObject();
        builder.writeString(message);
        builder.writeEndArray();
        builder.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    HttpServletResponse resp = response.get();
    resp.setHeader("X-Wf-Protocol-1", "http://meta.wildfirehq.org/Protocol/JsonStream/0.2");
    resp.setHeader("X-Wf-1-Plugin-1", "http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3");
    resp.setHeader("X-Wf-1-Structure-1", "http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1");
    int number = headerNumber.get();
    int offset = 0;
    int size = writer.getBuffer().length();
    while (size > offset) {
        StringBuilder header = new StringBuilder();
        if (offset == 0) {
            header.append(size);
        }
        header.append("|");
        int end = offset + 4000;
        if (end > size) {
            end = size;
        }
        header.append(writer.getBuffer().substring(offset, end));
        header.append("|");
        offset = end;
        if (offset != size) {
            header.append("\\");
        }
        resp.setHeader("X-Wf-1-1-1-" + number, header.toString());
        number++;
    }
    headerNumber.set(number);
}
Example 47
Project: nuxeo-webengine-master  File: JsonFactoryManagerImpl.java View source code
@Override
public JsonFactory createFactory() {
    JsonFactory factory = new JsonFactory();
    final ObjectMapper oc = new ObjectMapper(factory);
    final SimpleModule module = new SimpleModule("webengine", Version.unknownVersion()) {

        @Override
        public void setupModule(SetupContext context) {
            super.setupModule(context);
            context.addBeanSerializerModifier(new BeanSerializerModifier() {

                @Override
                public JsonSerializer<?> modifySerializer(SerializationConfig config, BasicBeanDescription beanDesc, JsonSerializer<?> serializer) {
                    if (!Throwable.class.isAssignableFrom(beanDesc.getBeanClass())) {
                        return super.modifySerializer(config, beanDesc, serializer);
                    }
                    return new ThrowableSerializer((BeanSerializer) serializer);
                }
            });
        }
    };
    oc.registerModule(module);
    factory.setCodec(oc);
    return factory;
}
Example 48
Project: opennaas-routing-nfv-master  File: UtilsTopology.java View source code
/**
     * Read the json file that contains the topology and store the nodes and
     * edges in the global variables.
     * @param topologyFilename
     * @param dijkstraCost
     * @return 
     */
public static TopologyInfo createAdjacencyMatrix(String topologyFilename, int dijkstraCost) {
    List<Vertex> nodes = new ArrayList<Vertex>();
    List<Edge> edges = new ArrayList<Edge>();
    try {
        JsonFactory f = new MappingJsonFactory();
        JsonParser jp = f.createJsonParser(new File(topologyFilename));
        JsonToken current = jp.nextToken();
        if (current != JsonToken.START_OBJECT) {
            Logger.getLogger(UtilsTopology.class.getName()).log(Level.SEVERE, null, "Error: root should be object: quiting.");
        }
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jp.getCurrentName();
            // move from field name to field value
            current = jp.nextToken();
            if (fieldName.equals("nodes")) {
                if (current == JsonToken.START_ARRAY) {
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        JsonNode node = jp.readValueAsTree();
                        int type;
                        if (node.get("type").getValueAsText().equals("sw")) {
                            type = 0;
                        } else {
                            type = 1;
                        }
                        Vertex v = new Vertex(node.get("id").getValueAsText(), node.get("dpid").getValueAsText(), type);
                        nodes.add(v);
                    }
                } else {
                    jp.skipChildren();
                }
            } else if (fieldName.equals("links")) {
                if (current == JsonToken.START_ARRAY) {
                    // For each of the records in the array
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        JsonNode link = jp.readValueAsTree();
                        String srcId = link.get("source").getValueAsText();
                        String dstId = link.get("target").getValueAsText();
                        int srcPort = Integer.parseInt(link.get("srcP").getValueAsText());
                        int dstPort = Integer.parseInt(link.get("dstP").getValueAsText());
                        Vertex srcV = null;
                        Vertex dstV = null;
                        for (Vertex v : nodes) {
                            if (v.getId().equals(srcId)) {
                                srcV = new Vertex(v.getId(), v.getDPID(), v.getType());
                            } else if (v.getId().equals(dstId)) {
                                dstV = new Vertex(v.getId(), v.getDPID(), v.getType());
                            }
                        }
                        Edge e = new Edge(link.get("id").getValueAsText(), srcV, dstV, dijkstraCost, srcPort, dstPort);
                        edges.add(e);
                        e = new Edge(link.get("id").getValueAsText() + "-", dstV, srcV, dijkstraCost, dstPort, srcPort);
                        edges.add(e);
                    }
                } else {
                    jp.skipChildren();
                }
            } else {
                jp.skipChildren();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(UtilsTopology.class.getName()).log(Level.SEVERE, null, ex);
    }
    TopologyInfo topoInfo = new TopologyInfo(nodes, edges);
    return topoInfo;
}
Example 49
Project: rce-master  File: PersistentSettingsServiceImpl.java View source code
private Map<String, String> readStore(String filename) {
    Map<String, String> result = new HashMap<String, String>();
    File storeFile = new File(storageDirectory, filename);
    if (!storeFile.exists()) {
        try {
            storeFile.createNewFile();
        } catch (IOException e) {
            LOGGER.warn("Could not create new persistent settings file: ", e);
        }
    }
    JsonFactory f = new JsonFactory();
    JsonParser jp;
    try {
        jp = f.createJsonParser(new File(storageDirectory, filename));
        jp.nextToken();
        while (jp.hasCurrentToken() && jp.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = jp.getCurrentName();
            if (jp.nextToken() == JsonToken.END_OBJECT) {
                break;
            } else {
                result.put(fieldname, jp.getText());
            }
        }
        jp.close();
    } catch (IOException e) {
        result = null;
        LOGGER.warn(ERROR_MSG_LOAD + "(This is normal if RCE is starting for the first time) :", e);
    }
    return result;
}
Example 50
Project: spatial-framework-for-hadoop-master  File: EnclosedBaseJsonRecordReader.java View source code
// Both Esri JSON and GeoJSON conveniently have "features"
@Override
public boolean next(LongWritable key, Text value) throws IOException {
    JsonToken token;
    // feature array
    if (parser == null) {
        parser = new JsonFactory().createJsonParser(inputStream);
        parser.setCodec(new ObjectMapper());
        token = parser.nextToken();
        while (token != null && !(token == JsonToken.START_ARRAY && parser.getCurrentName() != null && parser.getCurrentName().equals("features"))) {
            token = parser.nextToken();
        }
        // never found the features array
        if (token == null)
            return false;
    }
    key.set(parser.getCurrentLocation().getCharOffset());
    token = parser.nextToken();
    // this token should be a start object with no name
    if (token == null || !(token == JsonToken.START_OBJECT && parser.getCurrentName() == null))
        return false;
    JsonNode node = parser.readValueAsTree();
    value.set(node.toString());
    return true;
}
Example 51
Project: stratosphere-master  File: PreviewPlanDumpTest.java View source code
private void dump(Plan p) {
    try {
        List<DataSinkNode> sinks = PactCompiler.createPreOptimizedPlan(p);
        PlanJSONDumpGenerator dumper = new PlanJSONDumpGenerator();
        String json = dumper.getPactPlanAsJSON(sinks);
        JsonParser parser = new JsonFactory().createJsonParser(json);
        while (parser.nextToken() != null) ;
    } catch (JsonParseException e) {
        e.printStackTrace();
        Assert.fail("JSON Generator produced malformatted output: " + e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail("An error occurred in the test: " + e.getMessage());
    }
}
Example 52
Project: Web-Karma-master  File: AvroImport.java View source code
@Override
public Worksheet generateWorksheet() throws JSONException, IOException, KarmaException {
    DataFileReader<Void> schemareader = new DataFileReader<>(file, new GenericDatumReader<Void>());
    Schema schema = schemareader.getSchema();
    schemareader.close();
    DataFileReader<GenericRecord> reader = new DataFileReader<>(file, new GenericDatumReader<GenericRecord>(schema));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write('[');
    baos.write('\n');
    GenericDatumWriter<GenericRecord> writer = new GenericDatumWriter<>(reader.getSchema());
    while (reader.hasNext()) {
        GenericRecord record = reader.next();
        JsonEncoder encoder = EncoderFactory.get().jsonEncoder(reader.getSchema(), new JsonFactory().createJsonGenerator(baos)).configure(baos);
        writer.write(record, encoder);
        encoder.flush();
        if (reader.hasNext()) {
            baos.write(',');
        }
    }
    reader.close();
    baos.write('\n');
    baos.write(']');
    baos.flush();
    baos.close();
    String json = new String(baos.toByteArray());
    JsonImport jsonImport = new JsonImport(json, this.getFactory(), this.getWorksheet(), workspace, maxNumLines);
    return jsonImport.generateWorksheet();
}
Example 53
Project: webengine-master  File: JsonFactoryManagerImpl.java View source code
@Override
public JsonFactory createFactory() {
    JsonFactory factory = new JsonFactory();
    final ObjectMapper oc = new ObjectMapper(factory);
    final SimpleModule module = new SimpleModule("webengine", Version.unknownVersion()) {

        @Override
        public void setupModule(SetupContext context) {
            super.setupModule(context);
            context.addBeanSerializerModifier(new BeanSerializerModifier() {

                @Override
                public JsonSerializer<?> modifySerializer(SerializationConfig config, BasicBeanDescription beanDesc, JsonSerializer<?> serializer) {
                    if (!Throwable.class.isAssignableFrom(beanDesc.getBeanClass())) {
                        return super.modifySerializer(config, beanDesc, serializer);
                    }
                    return new ThrowableSerializer((BeanSerializer) serializer);
                }
            });
        }
    };
    oc.registerModule(module);
    factory.setCodec(oc);
    return factory;
}
Example 54
Project: wharf-master  File: JacksonFactory.java View source code
/**
     * Create the JSON factory
     *
     * @return JSON factory
     */
private static JsonFactory getFactory() {
    JsonFactory jsonFactory = new JsonFactory();
    //Do not auto-close target output when writing completes
    jsonFactory.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    //Do not auto-close source output when reading completes
    jsonFactory.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
    return jsonFactory;
}
Example 55
Project: appverse-web-master  File: CustomMappingJacksonHttpMessageConverter.java View source code
protected Object[] readInternal(Class<?>[] parameterTypes, String payload) throws Exception {
    JsonFactory jsonFactory = this.objectMapper.getJsonFactory();
    JsonParser jp = jsonFactory.createJsonParser(payload);
    JsonToken token;
    List<Object> lObjs = new ArrayList<Object>();
    int i = 0;
    while ((token = jp.nextToken()) != null) {
        switch(token) {
            case VALUE_NUMBER_INT:
            case VALUE_STRING:
            case START_OBJECT:
                Object obj = jp.readValueAs(parameterTypes[i]);
                lObjs.add(obj);
                i++;
                break;
            default:
                break;
        }
    }
    if (lObjs.size() != parameterTypes.length) {
        throw new Exception("Parsed parameters do not match requested types.");
    }
    Object[] parametersFound = lObjs.toArray();
    return parametersFound;
}
Example 56
Project: avro-master  File: RpcSendTool.java View source code
private void dumpJson(PrintStream out, Schema schema, Object datum) throws IOException {
    DatumWriter<Object> writer = new GenericDatumWriter<Object>(schema);
    JsonGenerator g = new JsonFactory().createJsonGenerator(out, JsonEncoding.UTF8);
    g.useDefaultPrettyPrinter();
    writer.write(datum, EncoderFactory.get().jsonEncoder(schema, g));
    g.flush();
    out.println();
    out.flush();
}
Example 57
Project: avro-ui-master  File: AvroUiSandboxServiceImpl.java View source code
@Override
public String getJsonStringFromRecord(RecordField field) throws AvroUiSandboxServiceException {
    try {
        GenericRecord record = FormAvroConverter.createGenericRecordFromRecordField(field);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(baos, JsonEncoding.UTF8);
        jsonGenerator.useDefaultPrettyPrinter();
        JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(record.getSchema(), jsonGenerator);
        DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<GenericRecord>(record.getSchema());
        datumWriter.write(record, jsonEncoder);
        jsonEncoder.flush();
        baos.flush();
        return new String(baos.toByteArray(), UTF8);
    } catch (Exception e) {
        throw Utils.handleException(e);
    }
}
Example 58
Project: bigpetstore-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 59
Project: cdh-mesos-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 60
Project: cdh3u3-with-mesos-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 61
Project: EasySOA-Incubation-master  File: JsonMessageReader.java View source code
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    JsonNode jsonNode = mapper.readValue(entityStream, JsonNode.class);
    if (jsonNode.isArray()) {
        return mapper.readValue(jsonNode, SoaNodeInformation[].class);
    } else {
        JsonNode idNode = jsonNode.get("id");
        if (idNode != null && idNode.has("type") && idNode.has("name")) {
            return mapper.readValue(jsonNode, SoaNodeInformation.class);
        } else if (jsonNode.has("soaNodeInformations")) {
            return mapper.readValue(jsonNode, SoaNodeInformations.class);
        } else if (jsonNode.has("result")) {
            return mapper.readValue(jsonNode, OperationResult.class);
        } else if (jsonNode.has("serviceInformations")) {
            return mapper.readValue(jsonNode, ServiceInformations.class);
        } else if (jsonNode.has("endpointInformations")) {
            return mapper.readValue(jsonNode, EndpointInformations.class);
        } else if (jsonNode.has("slaOrOlaIndicators")) {
            return mapper.readValue(jsonNode, SlaOrOlaIndicators.class);
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            JsonFactory factory = new JsonFactory(new ObjectMapper());
            JsonGenerator jsonGenerator = factory.createJsonGenerator(baos);
            jsonGenerator.useDefaultPrettyPrinter();
            jsonGenerator.writeObject(jsonNode);
            return baos.toString();
        }
    }
}
Example 62
Project: findmyiphone-master  File: FindMyDevice.java View source code
private static void writeDeviceLocations(Writer out, String username, String password) throws IOException {
    // Initialize the HTTP client
    DefaultHttpClient hc = new DefaultHttpClient();
    // Authorize with Apple's mobile me service
    HttpGet auth = new HttpGet("https://auth.apple.com/authenticate?service=DockStatus&realm=primary-me&formID=loginForm&username=" + username + "&password=" + password + "&returnURL=aHR0cHM6Ly9zZWN1cmUubWUuY29tL3dvL1dlYk9iamVjdHMvRG9ja1N0YXR1cy53b2Evd2EvdHJhbXBvbGluZT9kZXN0aW5hdGlvblVybD0vYWNjb3VudA%3D%3D");
    hc.execute(auth);
    auth.abort();
    // Pull the isc-secure.me.com cookie out so we can set the X-Mobileme-Isc header properly
    String isc = extractIscCode(hc);
    // Get access to the devices and find out their ids
    HttpPost devicemgmt = new HttpPost("https://secure.me.com/wo/WebObjects/DeviceMgmt.woa/?lang=en");
    devicemgmt.addHeader("X-Mobileme-Version", "1.0");
    devicemgmt.addHeader("X-Mobileme-Isc", isc);
    devicemgmt.setEntity(new UrlEncodedFormEntity(new ArrayList<NameValuePair>()));
    HttpResponse devicePage = hc.execute(devicemgmt);
    // Extract the device ids from their html encoded in json
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    devicePage.getEntity().writeTo(os);
    os.close();
    Matcher m = Pattern.compile("DeviceMgmt.deviceIdMap\\['[0-9]+'\\] = '([a-z0-9]+)';").matcher(new String(os.toByteArray()));
    List<String> deviceList = new ArrayList<String>();
    while (m.find()) {
        deviceList.add(m.group(1));
    }
    // For each available device, get the location
    JsonFactory jf = new JsonFactory();
    JsonGenerator jg = jf.createJsonGenerator(out);
    jg.writeStartObject();
    for (String device : deviceList) {
        HttpPost locate = new HttpPost("https://secure.me.com/wo/WebObjects/DeviceMgmt.woa/wa/LocateAction/locateStatus");
        locate.addHeader("X-Mobileme-Version", "1.0");
        locate.addHeader("X-Mobileme-Isc", isc);
        locate.setEntity(new StringEntity("postBody={\"deviceId\": \"" + device + "\", \"deviceOsVersion\": \"7A341\"}"));
        locate.setHeader("Content-type", "application/json");
        HttpResponse location = hc.execute(locate);
        InputStream inputStream = location.getEntity().getContent();
        JsonParser jp = jf.createJsonParser(inputStream);
        // ugly
        jp.nextToken();
        jg.writeFieldName(device);
        jg.copyCurrentStructure(jp);
        inputStream.close();
    }
    jg.close();
    out.close();
}
Example 63
Project: hadaps-master  File: RumenToSLSConverter.java View source code
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException {
    Reader input = new FileReader(inputFile);
    try {
        Writer output = new FileWriter(outputFile);
        try {
            ObjectMapper mapper = new ObjectMapper();
            ObjectWriter writer = mapper.defaultPrettyPrintingWriter();
            Iterator<Map> i = mapper.readValues(new JsonFactory().createJsonParser(input), Map.class);
            while (i.hasNext()) {
                Map m = i.next();
                output.write(writer.writeValueAsString(createSLSJob(m)) + EOL);
            }
        } finally {
            output.close();
        }
    } finally {
        input.close();
    }
}
Example 64
Project: hadoop-1.0.3-gpu-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 65
Project: hadoop-20-master  File: JsonUtils.java View source code
/**
   * This is a helper method which creates a JsonGenerator instance, for writing
   * the state of the ClusterManager to the state file. The JsonGenerator
   * instance writes to a compressed file if we have the compression flag
   * turned on.
   *
   * @param conf The CoronaConf instance to be used
   * @return The JsonGenerator instance to be used
   * @throws IOException
   */
public static JsonGenerator createJsonGenerator(CoronaConf conf) throws IOException {
    OutputStream outputStream = new FileOutputStream(conf.getCMStateFile());
    if (conf.getCMCompressStateFlag()) {
        outputStream = new GZIPOutputStream(outputStream);
    }
    ObjectMapper mapper = new ObjectMapper();
    JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
    jsonGenerator.setCodec(mapper);
    if (!conf.getCMCompressStateFlag()) {
        jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());
    }
    return jsonGenerator;
}
Example 66
Project: hadoop-20-warehouse-fix-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 67
Project: hadoop-20-warehouse-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 68
Project: hadoop-master  File: RumenToSLSConverter.java View source code
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException {
    try (Reader input = new InputStreamReader(new FileInputStream(inputFile), "UTF-8")) {
        try (Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) {
            ObjectMapper mapper = new ObjectMapper();
            ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
            Iterator<Map> i = mapper.readValues(new JsonFactory().createJsonParser(input), Map.class);
            while (i.hasNext()) {
                Map m = i.next();
                output.write(writer.writeValueAsString(createSLSJob(m)) + EOL);
            }
        }
    }
}
Example 69
Project: hadoop-on-lustre2-master  File: RumenToSLSConverter.java View source code
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException {
    Reader input = new FileReader(inputFile);
    try {
        Writer output = new FileWriter(outputFile);
        try {
            ObjectMapper mapper = new ObjectMapper();
            ObjectWriter writer = mapper.defaultPrettyPrintingWriter();
            Iterator<Map> i = mapper.readValues(new JsonFactory().createJsonParser(input), Map.class);
            while (i.hasNext()) {
                Map m = i.next();
                output.write(writer.writeValueAsString(createSLSJob(m)) + EOL);
            }
        } finally {
            output.close();
        }
    } finally {
        input.close();
    }
}
Example 70
Project: hadoop-release-2.6.0-master  File: PluginStoreTestUtils.java View source code
/**
   * Write timeline entities to a file system
   * @param entities
   * @param logPath
   * @param fs
   * @throws IOException
   */
static void writeEntities(TimelineEntities entities, Path logPath, FileSystem fs) throws IOException {
    FSDataOutputStream outStream = createLogFile(logPath, fs);
    JsonGenerator jsonGenerator = (new JsonFactory()).createJsonGenerator(outStream);
    jsonGenerator.setPrettyPrinter(new MinimalPrettyPrinter("\n"));
    ObjectMapper objMapper = createObjectMapper();
    for (TimelineEntity entity : entities.getEntities()) {
        objMapper.writeValue(jsonGenerator, entity);
    }
    outStream.close();
}
Example 71
Project: hadoop-src-research-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 72
Project: HadoopEKG-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 73
Project: HadoopUSC-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 74
Project: hdfs-cloudera-cdh3u3-production-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 75
Project: HDP-2.2-Patched-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 76
Project: hops-master  File: RumenToSLSConverter.java View source code
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException {
    Reader input = new FileReader(inputFile);
    try {
        Writer output = new FileWriter(outputFile);
        try {
            ObjectMapper mapper = new ObjectMapper();
            ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
            Iterator<Map> i = mapper.readValues(new JsonFactory().createJsonParser(input), Map.class);
            while (i.hasNext()) {
                Map m = i.next();
                output.write(writer.writeValueAsString(createSLSJob(m)) + EOL);
            }
        } finally {
            output.close();
        }
    } finally {
        input.close();
    }
}
Example 77
Project: JavaClientStoragePlugin-master  File: UploadProcessImpl.java View source code
@Override
public void run() {
    try {
        uploadProcessStartTime = System.nanoTime();
        HttpPut httpPut = new HttpPut(uploadURL);
        httpPut.setEntity(uploadHttpEntity);
        uploadState = UploadState.UPLOADING;
        HttpResponse httpResponse = httpClient.execute(httpPut);
        uploadingEndTime = System.nanoTime();
        System.out.println("[StorageService UploadProcessImpl] " + uploadURL);
        if (httpResponse.getStatusLine().getStatusCode() != 201 && httpResponse.getStatusLine().getStatusCode() != 202) {
            System.err.println("[StorageService UploadProcessImpl] " + httpResponse.getStatusLine().getStatusCode());
            return;
        }
        uploadState = UploadState.PUBLISHING;
        HttpEntity httpResponseEntity = httpResponse.getEntity();
        String successToken = "";
        JsonFactory jsonFactory = new JsonFactory();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        httpResponseEntity.writeTo(byteArrayOutputStream);
        byteArrayOutputStream.flush();
        JsonParser jsonParser = jsonFactory.createJsonParser(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
        jsonParser.nextToken();
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jsonParser.getCurrentName();
            jsonParser.nextToken();
            if ("successToken".equals(fieldName)) {
                successToken = jsonParser.getText();
            } else {
                return;
            }
        }
        jsonParser.close();
        if (successToken.equals("")) {
            return;
        }
        ClientResource clientResource = new ClientResource(uploadInfo.getAttachmentID().getUniqueResourceURL().toExternalForm() + "/lock?token=" + successToken);
        clientResource.setNext(restletClient);
        UserFactory.authenticateClientResource(userSession.getUser(), clientResource);
        clientResource.delete();
        System.out.println("[StorageService UploadProcessImpl] " + uploadInfo.getAttachmentID().getUniqueResourceURL().toExternalForm() + "/lock?token=" + successToken);
        if (clientResource.getStatus().getCode() != 204 && clientResource.getStatus().getCode() != 200 && clientResource.getStatus().getCode() != 202) {
            System.err.println("[StorageService UploadProcessImpl] " + clientResource.getStatus().getCode());
            return;
        }
        uploadProcessEndTime = System.nanoTime();
        uploadReport = new UploadReport(uploadInfo, uploadProcessStartTime, uploadProcessEndTime, uploadProcessStartTime, uploadingEndTime);
        uploadState = UploadState.FINISHED;
        uploadHttpEntity.consumeContent();
        httpResponseEntity.consumeContent();
    } catch (ClientProtocolException e) {
        return;
    } catch (IOException e) {
        return;
    } finally {
        if (uploadingEndTime == 0L) {
            uploadingEndTime = System.nanoTime();
        }
        if (uploadProcessEndTime == 0L) {
            uploadProcessEndTime = System.nanoTime();
        }
        if (uploadState != UploadState.FINISHED) {
            uploadState = UploadState.ABORTED;
        }
        uploadManager.finish(this);
        done = true;
        countDownLatch.countDown();
    }
}
Example 78
Project: mathosphere-master  File: TokenCounter.java View source code
private void processFile(InputStream in, Extractor tokens) throws IOException {
    JsonFactory jfactory = new JsonFactory();
    JsonParser jParser = jfactory.createJsonParser(in);
    String hash = "";
    if (jParser.nextToken() != JsonToken.START_ARRAY) {
        emitError("Expected a JSON array Unexpected token " + jParser.getText());
    }
    while (jParser.nextToken() != JsonToken.END_ARRAY) {
        if (jParser.getCurrentToken() == JsonToken.START_OBJECT) {
            jParser.nextToken();
            if (jParser.getCurrentName().equals("inputhash")) {
                jParser.nextToken();
                hash = jParser.getText();
                jParser.nextToken();
            } else {
                emitError("Missing inputhash " + jParser.getText());
            }
        } else {
            emitError("Unexpected token " + jParser.getText());
        }
        switch(jParser.getCurrentName()) {
            case "tokens":
            case "texvcinfo":
                if (jParser.nextToken() == JsonToken.START_ARRAY) {
                    while (jParser.nextToken() != JsonToken.END_ARRAY) {
                        tokens.addFromJson(jParser);
                    }
                } else if ((jParser.nextToken() == JsonToken.START_OBJECT)) {
                    if (jParser.nextToken() != JsonToken.END_OBJECT) {
                        //noinspection StatementWithEmptyBody
                        do {
                        } while (jParser.nextToken() != JsonToken.END_OBJECT);
                    }
                } else {
                    emitError("[ after texvcinfo expected in " + hash + " but got " + jParser.getText());
                }
                break;
            default:
                emitError("Unexpected token" + jParser.getText());
        }
        if (jParser.nextToken() != JsonToken.END_OBJECT) {
            emitError("Missing object end");
        }
    }
    jParser.close();
}
Example 79
Project: neo4j-enterprise-master  File: JsonReportWriter.java View source code
@Override
public void beginTimingProgress(long totalElementCount, long totalTimeNanos) throws IOException {
    ensureOpen(false);
    json = new JsonFactory().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, true).createJsonGenerator(new FileWriter(target));
    json.setPrettyPrinter(new DefaultPrettyPrinter());
    json.writeStartObject();
    {
        json.writeFieldName("config");
        json.writeStartObject();
        emitConfiguration();
        json.writeEndObject();
    }
    {
        json.writeFieldName("tuningConfig");
        json.writeStartObject();
        emitTuningConfiguration();
        json.writeEndObject();
    }
    {
        json.writeFieldName("total");
        json.writeStartObject();
        emitTime(totalElementCount, totalTimeNanos);
        json.writeEndObject();
    }
    json.writeFieldName("phases");
    json.writeStartArray();
}
Example 80
Project: n_e_b_u_l_a-master  File: TestJackson.java View source code
public static void readByObjectMapper() throws ClassNotFoundException {
    final int timeOuter = 10;
    final int timeIn = 1000;
    final int times = timeOuter * timeIn;
    long start = 0, end = 0;
    try {
        // can reuse, share
        ObjectMapper mapper = new ObjectMapper();
        // globally
        User user = mapper.readValue(new File("User.json"), User.class);
        long costUser, costMap, costJava, costJavaRaw;
        long costUserW, costMapW, costRawW, costJavaW, costJavaRawW;
        {
            // -- JAVA START --
            ByteArrayOutputStream bos = new ByteArrayOutputStream(2048);
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(user);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            ois.readObject();
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                bos.reset();
                oos = new ObjectOutputStream(bos);
                for (int j = 0; j < timeIn; j++) {
                    oos.writeObject(user);
                }
            }
            end = System.nanoTime();
            costJavaW = timeEverySecond(times, start, end);
            bis = new ByteArrayInputStream(bos.toByteArray());
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                bis.reset();
                ois = new ObjectInputStream(bis);
                for (int j = 0; j < timeIn; j++) {
                    user = (User) ois.readObject();
                }
            }
            end = System.nanoTime();
            costJava = timeEverySecond(times, start, end);
        // -- JAVA END --
        }
        {
            // -- JAVA START --
            user = mapper.readValue(new File("User.json"), User.class);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(2048);
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            user.writeExternal(oos);
            oos.writeObject(user);
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(bis);
            user.readExternal(ois);
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                bos.reset();
                oos = new ObjectOutputStream(bos);
                for (int j = 0; j < timeIn; j++) {
                    user.writeExternal(oos);
                }
            }
            end = System.nanoTime();
            costJavaRawW = timeEverySecond(times, start, end);
            bis = new ByteArrayInputStream(bos.toByteArray());
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                bis.reset();
                ois = new ObjectInputStream(bis);
                for (int j = 0; j < timeIn; j++) {
                    user.readExternal(ois);
                }
            }
            end = System.nanoTime();
            costJavaRaw = timeEverySecond(times, start, end);
        // -- JAVA END --
        }
        {
            ByteArrayOutputStream os = new ByteArrayOutputStream(2048);
            mapper.writeValue(os, user);
            ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
            Map<?, ?> mp = mapper.readValue(is, Map.class);
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                os.reset();
                for (int j = 0; j < timeIn; j++) {
                    mapper.writeValue(os, user);
                }
            }
            end = System.nanoTime();
            costUserW = timeEverySecond(times, start, end);
            is = new ByteArrayInputStream(os.toByteArray());
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                for (int j = 0; j < timeIn; j++) {
                    is.reset();
                    user = mapper.readValue(is, User.class);
                }
            }
            end = System.nanoTime();
            costUser = timeEverySecond(times, start, end);
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                os.reset();
                for (int j = 0; j < timeIn; j++) {
                    mapper.writeValue(os, mp);
                }
            }
            end = System.nanoTime();
            costMapW = timeEverySecond(times, start, end);
            is = new ByteArrayInputStream(os.toByteArray());
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                for (int j = 0; j < timeIn; j++) {
                    is.reset();
                    mp = mapper.readValue(is, Map.class);
                }
            }
            end = System.nanoTime();
            costMap = timeEverySecond(times, start, end);
            UserSerialize us = new UserSerialize();
            JsonFactory f = new JsonFactory();
            JsonGenerator g = f.createJsonGenerator(os);
            start = System.nanoTime();
            for (int i = 0; i < timeOuter; i++) {
                os.reset();
                for (int j = 0; j < timeIn; j++) {
                    us.writeMedia(g, user);
                }
            }
            end = System.nanoTime();
            costRawW = timeEverySecond(times, start, end);
        }
        System.out.println("costUser : " + costUser + " costMap : " + costMap + " costJava : " + costJava + " costJavaRaw : " + costJavaRaw);
        System.out.println("costUserW: " + costUserW + " costMapW: " + costMapW + " costJavaW: " + costJavaW + " costRawW: " + costRawW + " costJavaRawW: " + costJavaRawW);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Example 81
Project: org.ops4j.pax.url-master  File: AssemblyDescriptorParser.java View source code
/**
     * Reads descriptor file.
     *
     * @param url descriptor file url
     *
     * @throws IOException - If a problem encountered while parsing descriptor file
     */
private void parseDescriptor(final URL url) throws IOException {
    final JsonFactory jFactory = new JsonFactory();
    jFactory.enable(JsonParser.Feature.ALLOW_COMMENTS);
    final JsonParser jp = jFactory.createJsonParser(url);
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        throw new IOException(String.format("Descriptor [%s] not in JSON format", url.toExternalForm()));
    }
    final Collection<Source> sources = new HashSet<Source>();
    try {
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            final String currentName = jp.getCurrentName();
            jp.nextToken();
            if ("manifest".equals(currentName)) {
                m_manifest = jp.getText();
            } else if ("assembly".equals(currentName)) {
                sources.addAll(parseAssembly(jp));
            } else if ("mergePolicy".equals(currentName)) {
                m_mergePolicy = "last".equalsIgnoreCase(jp.getText()) ? MergePolicy.LAST : MergePolicy.FIRST;
            }
        }
    } finally {
        jp.close();
    }
    m_sources = sources.toArray(new Source[sources.size()]);
}
Example 82
Project: phresco-master  File: JSTestResultHandler.java View source code
/**
	 * Handle a test result.
	 */
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    if (target.equals("/testResults") && request.getMethod().equals("POST") && request.getContentType().contains("application/json")) {
        JSTestResult jsTestResult = null;
        JsonFactory f = new JsonFactory();
        JsonParser jp = f.createJsonParser(request.getReader());
        String testUrl = null;
        Integer failures = null;
        Integer passes = null;
        String message = null;
        try {
            // will return JsonToken.START_OBJECT (verify?)
            jp.nextToken();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String fieldname = jp.getCurrentName();
                // move to value, or
                jp.nextToken();
                // START_OBJECT/START_ARRAY
                if (// contains an object
                fieldname.equals("testUrl")) {
                    testUrl = jp.getText();
                } else if (fieldname.equals("failures")) {
                    failures = jp.getIntValue();
                } else if (fieldname.equals("passes")) {
                    passes = jp.getIntValue();
                } else if (fieldname.equals("message")) {
                    message = jp.getText();
                }
            }
        } finally {
            jp.close();
        }
        // Store any failures.
        if (testUrl != null && failures != null && passes != null && message != null) {
            jsTestResult = new JSTestResult();
            jsTestResult.failures = failures;
            jsTestResult.message = message;
            jsTestResult.passes = passes;
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
        response.setContentType("application/json");
        baseRequest.setHandled(true);
        // Minimally signal any threads to re-look at things. Store results
        // if we received them.
        jsTestResultsLock.lock();
        try {
            if (jsTestResult != null) {
                jsTestResults.put(testUrl, jsTestResult);
            }
            newJsTestResults.signalAll();
        } finally {
            jsTestResultsLock.unlock();
        }
    }
}
Example 83
Project: prime-master  File: Utilities.java View source code
private static List<Image> getRandomImages(Context context, String source) {
    List<Image> images = new ArrayList<Image>();
    try {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        String jsonTag = URLEncoder.encode(source);
        String rawJsonStringStream = sharedPreferences.getString(jsonTag, null);
        if (rawJsonStringStream == null) {
            rawJsonStringStream = toString(new URL(source).openStream());
            sharedPreferences.edit().putString(jsonTag, rawJsonStringStream).commit();
        }
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jsonParser = jsonFactory.createJsonParser(rawJsonStringStream);
        JsonToken current;
        current = jsonParser.nextToken();
        if (current != JsonToken.START_OBJECT) {
            return images;
        }
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
            String fieldName = jsonParser.getCurrentName();
            current = jsonParser.nextToken();
            if ("photos".equals(fieldName)) {
                if (current == JsonToken.START_ARRAY) {
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        fieldName = jsonParser.getCurrentName();
                        jsonParser.nextToken();
                        Image image = new Image();
                        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                            fieldName = jsonParser.getCurrentName();
                            if ("photo_title".equals(fieldName)) {
                                image.title = jsonParser.getText();
                            } else if ("photo_file_url".equals(fieldName)) {
                                image.imageURL = jsonParser.getText();
                            }
                        }
                        images.add(image);
                    }
                } else {
                    jsonParser.skipChildren();
                }
            } else {
                jsonParser.skipChildren();
            }
        }
        jsonParser.close();
        if (images.size() == 0) {
            sharedPreferences.edit().putString(jsonTag, null).commit();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return images;
}
Example 84
Project: RDFS-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 85
Project: reddit-is-fun-master  File: LoginTask.java View source code
/**
     * On success stores the session cookie and modhash in your RedditSettings.
     * On failure does not modify RedditSettings. 
     * Should be called from a background thread.
     * @return Error message, or null on success
     */
private Boolean doLogin(String username, String password, RedditSettings settings, HttpClient client, Context context) {
    String status = "";
    String userError = "Error logging in. Please try again.";
    HttpEntity entity = null;
    try {
        // Construct data
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("user", username.toString()));
        nvps.add(new BasicNameValuePair("passwd", password.toString()));
        nvps.add(new BasicNameValuePair("api_type", "json"));
        HttpPost httppost = new HttpPost(Constants.REDDIT_LOGIN_URL);
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        // Set timeout to 45 seconds for login
        HttpParams params = httppost.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 45000);
        HttpConnectionParams.setSoTimeout(params, 45000);
        // Perform the HTTP POST request
        HttpResponse response = client.execute(httppost);
        status = response.getStatusLine().toString();
        if (!status.contains("OK")) {
            throw new HttpException(status);
        }
        entity = response.getEntity();
        BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
        String line = in.readLine();
        in.close();
        entity.consumeContent();
        if (StringUtils.isEmpty(line)) {
            throw new HttpException("No content returned from login POST");
        }
        if (Constants.LOGGING)
            Common.logDLong(TAG, line);
        if (RedditIsFunHttpClientFactory.getCookieStore().getCookies().isEmpty())
            throw new HttpException("Failed to login: No cookies");
        final JsonFactory jsonFactory = new JsonFactory();
        final JsonParser jp = jsonFactory.createJsonParser(line);
        // Go to the errors
        while (jp.nextToken() != JsonToken.FIELD_NAME || !Constants.JSON_ERRORS.equals(jp.getCurrentName())) ;
        if (jp.nextToken() != JsonToken.START_ARRAY)
            throw new IllegalStateException("Login: expecting errors START_ARRAY");
        if (jp.nextToken() != JsonToken.END_ARRAY) {
            if (line.contains("WRONG_PASSWORD")) {
                userError = "Bad password.";
                throw new Exception("Wrong password");
            } else {
                // Could parse for error code and error description but using whole line is easier.
                throw new Exception(line);
            }
        }
        // Get modhash
        while (jp.nextToken() != JsonToken.FIELD_NAME || !Constants.JSON_MODHASH.equals(jp.getCurrentName())) ;
        jp.nextToken();
        settings.setModhash(jp.getText());
        // Could grab cookie from JSON too, but it lacks expiration date and stuff. So grab from HttpClient.
        List<Cookie> cookies = RedditIsFunHttpClientFactory.getCookieStore().getCookies();
        for (Cookie c : cookies) {
            if (c.getName().equals("reddit_session")) {
                settings.setRedditSessionCookie(c);
                break;
            }
        }
        settings.setUsername(username);
        CookieSyncManager.getInstance().sync();
        CacheInfo.invalidateAllCaches(context);
        return true;
    } catch (Exception e) {
        mUserError = userError;
        if (entity != null) {
            try {
                entity.consumeContent();
            } catch (Exception e2) {
                if (Constants.LOGGING)
                    Log.e(TAG, "entity.consumeContent()", e);
            }
        }
        if (Constants.LOGGING)
            Log.e(TAG, "doLogin()", e);
    }
    return false;
}
Example 86
Project: seldon-server-master  File: ExternalPluginServer.java View source code
@Override
public JsonNode execute(JsonNode payload, OptionsHolder options) {
    long timeNow = System.currentTimeMillis();
    URI uri = URI.create(options.getStringOption(URL_PROPERTY_NAME));
    try {
        URIBuilder builder = new URIBuilder().setScheme("http").setHost(uri.getHost()).setPort(uri.getPort()).setPath(uri.getPath()).setParameter("json", payload.toString());
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new APIException(APIException.GENERIC_ERROR);
    }
    HttpContext context = HttpClientContext.create();
    HttpGet httpGet = new HttpGet(uri);
    try {
        if (logger.isDebugEnabled())
            logger.debug("Requesting " + httpGet.getURI().toString());
        CloseableHttpResponse resp = httpClient.execute(httpGet, context);
        if (resp.getStatusLine().getStatusCode() == 200) {
            JsonFactory factory = mapper.getJsonFactory();
            JsonParser parser = factory.createJsonParser(resp.getEntity().getContent());
            JsonNode actualObj = mapper.readTree(parser);
            if (logger.isDebugEnabled())
                logger.debug("External prediction server took " + (System.currentTimeMillis() - timeNow) + "ms");
            return actualObj;
        } else {
            logger.error("Couldn't retrieve prediction from external prediction server -- bad http return code: " + resp.getStatusLine().getStatusCode());
            throw new APIException(APIException.GENERIC_ERROR);
        }
    } catch (IOException e) {
        logger.error("Couldn't retrieve prediction from external prediction server - ", e);
        throw new APIException(APIException.GENERIC_ERROR);
    }
}
Example 87
Project: solutions-geoevent-java-master  File: PolygonProcessor.java View source code
private com.esri.ges.spatial.Geometry constructJsonGeometry(String jsongeo) {
    try {
        String jsonOut = jsongeo;
        JsonFactory jf = new JsonFactory();
        JsonParser json = jf.createJsonParser(jsongeo);
        MapGeometry mgeo = GeometryEngine.jsonToGeometry(json);
        if (!mgeo.getSpatialReference().equals(srOut)) {
            com.esri.core.geometry.Geometry geo = mgeo.getGeometry();
            com.esri.core.geometry.Geometry projGeo = GeometryEngine.project(geo, srIn, srOut);
            jsonOut = GeometryEngine.geometryToJson(srOut, projGeo);
        }
        return spatial.fromJson(jsonOut);
    } catch (JsonParseException ex) {
        LOG.error("Unable to parse json");
        LOG.error(ex.getMessage());
        LOG.error(ex.getStackTrace());
        return null;
    } catch (IOException ex) {
        LOG.error(ex.getMessage());
        LOG.error(ex.getStackTrace());
        return null;
    } catch (GeometryException ex) {
        LOG.error(ex.getMessage());
        LOG.error(ex.getStackTrace());
        return null;
    }
}
Example 88
Project: tuscany-sca-2.x-master  File: JacksonHelper.java View source code
public static String toString(JsonNode node) {
    try {
        JsonFactory jsonFactory = new JsonFactory();
        StringWriter sw = new StringWriter();
        JsonGenerator generator = jsonFactory.createJsonGenerator(sw);
        generator.writeTree(node);
        return sw.toString();
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Example 89
Project: WSPerfLab-master  File: ServiceResponseBuilder.java View source code
public static ByteArrayOutputStream buildTestBResponse(JsonFactory jsonFactory, BackendResponse response) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos);
    jsonGenerator.writeStartObject();
    // delay values of each response
    jsonGenerator.writeArrayFieldStart("delay");
    writeTuple(jsonGenerator, "a", response.getDelay());
    jsonGenerator.writeEndArray();
    // itemSize values of each response
    jsonGenerator.writeArrayFieldStart("itemSize");
    writeTuple(jsonGenerator, "a", response.getItemSize());
    jsonGenerator.writeEndArray();
    // numItems values of each response
    jsonGenerator.writeArrayFieldStart("numItems");
    writeTuple(jsonGenerator, "a", response.getNumItems());
    jsonGenerator.writeEndArray();
    // all items from responses
    jsonGenerator.writeArrayFieldStart("items");
    addItemsFromResponse(jsonGenerator, response);
    jsonGenerator.writeEndArray();
    jsonGenerator.writeEndObject();
    jsonGenerator.close();
    return bos;
}
Example 90
Project: yarn-comment-master  File: TestHistograms.java View source code
public static void main(String[] args) throws IOException {
    final Configuration conf = new Configuration();
    final FileSystem lfs = FileSystem.getLocal(conf);
    for (String arg : args) {
        Path filePath = new Path(arg).makeQualified(lfs);
        String fileName = filePath.getName();
        if (fileName.startsWith("input")) {
            LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
            String testName = fileName.substring("input".length());
            Path goldFilePath = new Path(filePath.getParent(), "gold" + testName);
            ObjectMapper mapper = new ObjectMapper();
            JsonFactory factory = mapper.getJsonFactory();
            FSDataOutputStream ostream = lfs.create(goldFilePath, true);
            JsonGenerator gen = factory.createJsonGenerator(ostream, JsonEncoding.UTF8);
            gen.useDefaultPrettyPrinter();
            gen.writeObject(newResult);
            gen.close();
        } else {
            System.err.println("Input file not started with \"input\". File " + fileName + " skipped.");
        }
    }
}
Example 91
Project: aws-sdk-for-android-master  File: SignatureChecker.java View source code
private Map<String, String> parseJSON(String jsonmessage) {
    Map<String, String> parsed = new HashMap<String, String>();
    JsonFactory jf = new JsonFactory();
    try {
        JsonParser parser = jf.createJsonParser(jsonmessage);
        //shift past the START_OBJECT that begins the JSON
        parser.nextToken();
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            String fieldname = parser.getCurrentName();
            // move to value, or START_OBJECT/START_ARRAY
            parser.nextToken();
            String value = parser.getText();
            parsed.put(fieldname, value);
        }
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
    }
    return parsed;
}
Example 92
Project: CMM-data-grabber-master  File: DatasetMetadata.java View source code
public void serialize(Writer writer) throws IOException {
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory jf = new JsonFactory();
        JsonGenerator jg = jf.createJsonGenerator(writer);
        jg.useDefaultPrettyPrinter();
        mapper.writeValue(jg, this);
    } catch (JsonParseException ex) {
        throw new PaulException(ex);
    } catch (JsonMappingException ex) {
        throw new PaulException(ex);
    }
}
Example 93
Project: dtgov-master  File: UiConfigurationServlet.java View source code
/**
     * Called to generate the JSON.
     * @param request
     * @param config
     * @throws Exception
     */
protected static String generateJSONConfig(HttpServletRequest request, DtgovUIConfig config, IDtgovClient client) throws Exception {
    StringWriter json = new StringWriter();
    JsonFactory f = new JsonFactory();
    JsonGenerator g = f.createJsonGenerator(json);
    g.useDefaultPrettyPrinter();
    g.writeStartObject();
    // Some s-ramp UI/browser integration settings
    //$NON-NLS-1$
    g.writeObjectFieldStart("srampui");
    //$NON-NLS-1$
    g.writeStringField("urlBase", config.getConfiguration().getString(DtgovUIConfig.SRAMP_UI_URL_BASE, autoGenerateSrampUiUrlBase(request)));
    g.writeEndObject();
    // Auth settings
    //$NON-NLS-1$
    g.writeObjectFieldStart("auth");
    //$NON-NLS-1$
    g.writeStringField("currentUser", request.getRemoteUser());
    //$NON-NLS-1$
    g.writeBooleanField("isAdmin", AuthUtils.isOverlordAdmin(request));
    g.writeEndObject();
    //$NON-NLS-1$
    g.writeObjectFieldStart("deployments");
    // Pull in any configured deployment types.
    @SuppressWarnings("unchecked") Iterator<String> typeKeys = config.getConfiguration().getKeys(DtgovUIConfig.DEPLOYMENT_TYPE_PREFIX);
    int count = 0;
    //$NON-NLS-1$
    g.writeObjectFieldStart("types");
    while (typeKeys.hasNext()) {
        String typeKey = typeKeys.next();
        String value = config.getConfiguration().getString(typeKey);
        if (value.contains(":")) {
            //$NON-NLS-1$
            int idx = value.indexOf(':');
            String label = value.substring(0, idx);
            String type = value.substring(idx + 1);
            g.writeStringField(label, type);
            count++;
        }
    }
    if (count == 0) {
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.TypeSwitchYard"), "ext/SwitchYardApplication");
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.TypeWebApp"), "ext/JavaWebApplication");
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.TypeJ2EEApp"), "ext/JavaEnterpriseApplication");
    }
    g.writeEndObject();
    // Pull in any configured deployment stages.
    List<DeploymentStage> stages = config.getStages();
    //$NON-NLS-1$
    g.writeObjectFieldStart("stages");
    if (stages.isEmpty()) {
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.StageDevelopment"), "http://www.jboss.org/overlord/deployment-status.owl#Dev");
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.StageQA"), "http://www.jboss.org/overlord/deployment-status.owl#Qa");
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.StageProd"), "http://www.jboss.org/overlord/deployment-status.owl#Prod");
    } else {
        for (DeploymentStage deploymentStage : stages) {
            g.writeStringField(deploymentStage.getLabel(), deploymentStage.getClassifier());
        }
    }
    g.writeEndObject();
    g.writeEndObject();
    //$NON-NLS-1$
    g.writeObjectFieldStart("workflow");
    // Pull in any configured workflow property default types.
    @SuppressWarnings("unchecked") Iterator<String> workflowPropertyTypes = config.getConfiguration().getKeys(DtgovUIConfig.WORKFLOW_PROPERTY_PREFIX);
    count = 0;
    //$NON-NLS-1$
    g.writeObjectFieldStart("propertyTypes");
    if (workflowPropertyTypes != null) {
        while (workflowPropertyTypes.hasNext()) {
            String workflowPropertyTypeKey = workflowPropertyTypes.next();
            String value = config.getConfiguration().getString(workflowPropertyTypeKey);
            if (value.contains(":")) {
                //$NON-NLS-1$
                int idx = value.indexOf(':');
                String label = value.substring(0, idx);
                String type = value.substring(idx + 1);
                g.writeStringField(label, type);
                count++;
            }
        }
    }
    if (count == 0) {
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.workflow.property.deploymentUrl"), "{governance.url}/rest/deploy/{target}/{uuid}");
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.workflow.property.notificationUrl"), "{governance.url}/rest/notify/email/{group}/deployed/{target}/{uuid}");
        //$NON-NLS-1$ //$NON-NLS-2$
        g.writeStringField(Messages.i18n.format("UiConfigurationServlet.workflow.property.updateMetadataUrl"), "{governance.url}/rest/update/{name}/{value}/{uuid}");
    }
    g.writeEndObject();
    g.writeEndObject();
    // Target types
    //$NON-NLS-1$
    g.writeObjectFieldStart("target");
    // Pull in any configured target property default types.
    TargetType[] targetTypes = TargetType.values();
    count = 0;
    //$NON-NLS-1$
    g.writeObjectFieldStart("types");
    if (targetTypes != null) {
        for (int i = 0; i < targetTypes.length; i++) {
            TargetType targetType = targetTypes[i];
            //$NON-NLS-1$
            String label = Messages.i18n.format("target.type." + targetType.getValue());
            g.writeStringField(label, targetType.getValue());
        }
    }
    g.writeEndObject();
    g.writeEndObject();
    List<Deployer> customDeployers = null;
    if (client != null) {
        customDeployers = client.getCustomDeployerNames();
    }
    // Target types
    //$NON-NLS-1$
    g.writeObjectFieldStart("customDeployers");
    if (customDeployers != null && customDeployers.size() > 0) {
        for (Deployer customDeployer : customDeployers) {
            g.writeStringField(customDeployer.getName(), customDeployer.getName());
        }
    }
    g.writeEndObject();
    g.flush();
    g.close();
    return json.toString();
}
Example 94
Project: ecore-master  File: DockerUtilss.java View source code
/**
   * Parse String to Json data.
   */
public JsonNode jsonify(final String jsonString) {
    if (((jsonString != null) || (jsonString == ""))) {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = mapper.getJsonFactory();
        JsonParser parser = null;
        JsonNode node = null;
        try {
            JsonParser _createJsonParser = factory.createJsonParser(jsonString);
            parser = _createJsonParser;
        } catch (final Throwable _t) {
            if (_t instanceof IOException) {
                final IOException e = (IOException) _t;
                e.printStackTrace();
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        }
        try {
            JsonNode _readTree = mapper.readTree(parser);
            node = _readTree;
        } catch (final Throwable _t_1) {
            if (_t_1 instanceof IOException) {
                final IOException e_1 = (IOException) _t_1;
                e_1.printStackTrace();
            } else {
                throw Exceptions.sneakyThrow(_t_1);
            }
        }
        return node;
    }
    return null;
}
Example 95
Project: elw-master  File: RunnableLoadTask.java View source code
protected static ResponseList readRespList(InputStream is) throws IOException {
    JsonFactory jf = new JsonFactory();
    JsonParser jp = jf.createJsonParser(is);
    ResponseList response = new ResponseList();
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        throw new IllegalStateException("expecting start object");
    }
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        // move to START_OBJECT
        jp.nextToken();
        if ("success".equals(fieldname)) {
            response.setSuccess(Boolean.parseBoolean(jp.getText()));
        }
        if ("message".equals(fieldname)) {
            //	LATER this returns 'null' on me!
            response.setMessage(jp.getText());
        }
        if ("data".equals(fieldname)) {
            final Map<String, Map<String, String[]>> dataMap = new TreeMap<String, Map<String, String[]>>();
            while (jp.nextToken() != JsonToken.END_OBJECT) {
                String dataKeySlot = jp.getCurrentName();
                // move to START_OBJECT
                jp.nextToken();
                final Map<String, String[]> scopeToIdList = new TreeMap<String, String[]>();
                while (jp.nextToken() != JsonToken.END_OBJECT) {
                    String dataKeyScope = jp.getCurrentName();
                    // move to START_ARRAY
                    jp.nextToken();
                    List<String> idList = new ArrayList<String>();
                    while (jp.nextToken() != JsonToken.END_ARRAY) {
                        idList.add(jp.getText());
                    }
                    scopeToIdList.put(dataKeyScope, idList.toArray(new String[idList.size()]));
                }
                dataMap.put(dataKeySlot, scopeToIdList);
            }
            response.setData(dataMap);
        }
    }
    return response;
}
Example 96
Project: gyrex-jaxrs-application-master  File: Stax2JsonFactory.java View source code
public static XMLStreamWriter createWriter(Writer writer, JSONConfiguration config, boolean writingList) throws IOException {
    switch(config.getNotation()) {
        case NATURAL:
            final JsonGenerator rawGenerator = new JsonFactory().createJsonGenerator(writer);
            if (config.isHumanReadableFormatting()) {
                rawGenerator.useDefaultPrettyPrinter();
            }
            final JsonGenerator bodyGenerator = writingList ? JacksonArrayWrapperGenerator.createArrayWrapperGenerator(rawGenerator, config.isRootUnwrapping() ? 0 : 1) : rawGenerator;
            if (config.isRootUnwrapping()) {
                return new Stax2JacksonWriter(JacksonRootStrippingGenerator.createRootStrippingGenerator(bodyGenerator, writingList ? 2 : 1), config);
            } else {
                return new Stax2JacksonWriter(bodyGenerator, config);
            }
        case MAPPED:
            return JsonXmlStreamWriter.createWriter(writer, config);
        case BADGERFISH:
            return new BadgerFishXMLStreamWriter(writer);
        case MAPPED_JETTISON:
            Configuration jmConfig;
            if (null == config.getXml2JsonNs()) {
                jmConfig = new Configuration();
            } else {
                jmConfig = new Configuration(config.getXml2JsonNs());
            }
            return new MappedXMLStreamWriter(new MappedNamespaceConvention(jmConfig), writer);
        default:
            return null;
    }
}
Example 97
Project: io-master  File: BarFileUtils.java View source code
/**
     * barファイルエントリ�らJSONファイルを読�込む.
     * @param <T> JSONMappedObject
     * @param inStream barファイルエントリ�InputStream
     * @param entryName entryName
     * @param clazz clazz
     * @return JSONファイル�ら読�込ん�オブジェクト
     * @throws IOException JSONファイル読�込�エラー
     */
public static <T> T readJsonEntry(InputStream inStream, String entryName, Class<T> clazz) throws IOException {
    JsonParser jp = null;
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory f = new JsonFactory();
    jp = f.createJsonParser(inStream);
    // JSONルート�素("{")
    JsonToken token = jp.nextToken();
    Pattern formatPattern = Pattern.compile(".*/+(.*)");
    Matcher formatMatcher = formatPattern.matcher(entryName);
    String jsonName = formatMatcher.replaceAll("$1");
    T json = null;
    if (token == JsonToken.START_OBJECT) {
        try {
            json = mapper.readValue(jp, clazz);
        } catch (UnrecognizedPropertyException ex) {
            throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
        }
    } else {
        throw DcCoreException.BarInstall.JSON_FILE_FORMAT_ERROR.params(jsonName);
    }
    return json;
}
Example 98
Project: jvm-tools-master  File: MxDumpCmd.java View source code
@Override
public void run() {
    try {
        ObjectName q = null;
        if (query != null) {
            q = new ObjectName(query);
        }
        MBeanServerConnection jmx = conn.getMServer();
        JsonFactory jsonFactory = new JsonFactory();
        JsonGenerator jg = jsonFactory.createJsonGenerator(System.out);
        jg.useDefaultPrettyPrinter();
        jg.writeStartObject();
        listBeans(q, jg, jmx);
        jg.writeEndObject();
        jg.close();
        System.out.println();
        System.out.flush();
    } catch (Exception e) {
        host.fail(e.toString());
    }
}
Example 99
Project: mbox_tools-master  File: Converter.java View source code
/**
     * Convert mail to JSON. JSON output encoding is hardcoded to UTF-*.
     * Whatever is in metadata is added to or override result JSON.
     * @param mail
     * @param metadata
     * @param prettyPrint
     * @return
     */
public static String toJSON(Mail mail, Map<String, String> metadata, boolean prettyPrint) {
    String json = null;
    OutputStream os = new ByteArrayOutputStream();
    JsonGenerator generator = null;
    try {
        generator = new JsonFactory().createJsonGenerator(os, JsonEncoding.UTF8);
        generator.writeStartObject();
        if (prettyPrint == true) {
            generator.useDefaultPrettyPrinter();
        }
        if (mail.author_name() != null && !hasKey(metadata, "author_name"))
            generator.writeStringField("author_name", mail.author_name());
        if (mail.author_email() != null && !hasKey(metadata, "author_email"))
            generator.writeStringField("author_email", mail.author_email());
        if (mail.to() != null && !hasKey(metadata, "to")) {
            generator.writeArrayFieldStart("to");
            for (String to : mail.to()) {
                generator.writeString(to);
            }
            generator.writeEndArray();
        }
        if (mail.subject_original() != null && !hasKey(metadata, "subject_original"))
            generator.writeStringField("subject_original", mail.subject_original());
        if (mail.subject() != null && !hasKey(metadata, "subject"))
            generator.writeStringField("subject", mail.subject());
        if (mail.dateUTC() != null && !hasKey(metadata, "date"))
            generator.writeStringField("date", mail.dateUTC());
        if (mail.message_id_original() != null && !hasKey(metadata, "message_id_original"))
            generator.writeStringField("message_id_original", mail.message_id_original());
        if (mail.message_id() != null && !hasKey(metadata, "message_id"))
            generator.writeStringField("message_id", mail.message_id());
        if (mail.references() != null && !hasKey(metadata, "references")) {
            generator.writeArrayFieldStart("references");
            for (String ref : mail.references()) {
                generator.writeString(ref);
            }
            generator.writeEndArray();
        }
        if (mail.in_reply_to() != null && !hasKey(metadata, "in_reply_to"))
            generator.writeStringField("in_reply_to", mail.in_reply_to());
        if (mail.message_snippet() != null && !hasKey(metadata, "message_snippet"))
            generator.writeStringField("message_snippet", mail.message_snippet());
        if (mail.first_text_message() != null && !hasKey(metadata, "first_text_message"))
            generator.writeStringField("first_text_message", mail.first_text_message());
        if (mail.first_text_message_without_quotes() != null && !hasKey(metadata, "first_text_message_without_quotes"))
            generator.writeStringField("first_text_message_without_quotes", mail.first_text_message_without_quotes());
        if (mail.first_html_message() != null && !hasKey(metadata, "first_html_message"))
            generator.writeStringField("first_html_message", mail.first_html_message());
        if (mail.text_messages() != null && mail.text_messages().length > 0 && !hasKey(metadata, "text_messages")) {
            generator.writeArrayFieldStart("text_messages");
            for (String part : mail.text_messages()) {
                generator.writeString(part);
            }
            generator.writeEndArray();
        }
        if (mail.text_messages_cnt() != null && !hasKey(metadata, "text_messages_cnt"))
            generator.writeNumberField("text_messages_cnt", mail.text_messages_cnt());
        if (mail.html_messages() != null && mail.html_messages().length > 0 && !hasKey(metadata, "html_messages")) {
            generator.writeArrayFieldStart("html_messages");
            for (String part : mail.html_messages()) {
                generator.writeString(part);
            }
            generator.writeEndArray();
        }
        if (mail.html_messages_cnt() != null && !hasKey(metadata, "html_messages_cnt"))
            generator.writeNumberField("html_messages_cnt", mail.html_messages_cnt());
        if (mail.message_attachments() != null && mail.message_attachments().length > 0 && !hasKey(metadata, "message_attachments")) {
            generator.writeArrayFieldStart("message_attachments");
            for (MailAttachment atchm : mail.message_attachments()) {
                generator.writeStartObject();
                generator.writeStringField("content_type", atchm.getContentType());
                generator.writeStringField("filename", atchm.getFileName());
                generator.writeStringField("content", atchm.getContent());
                generator.writeEndObject();
            }
            generator.writeEndArray();
        }
        if (mail.message_attachments_cnt() != null && !hasKey(metadata, "message_attachments_cnt"))
            generator.writeNumberField("message_attachments_cnt", mail.message_attachments_cnt());
        if (metadata != null && !metadata.isEmpty()) {
            for (String key : metadata.keySet()) {
                if (key.trim().length() > 0) {
                    generator.writeStringField(key, metadata.get(key));
                }
            }
        }
        generator.writeEndObject();
        generator.close();
        json = os.toString();
    } catch (IOException e) {
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (IOException e) {
        }
    }
    return json;
}
Example 100
Project: OpenRefine-master  File: Row.java View source code
public static Row loadStreaming(String s, Pool pool) throws Exception {
    JsonFactory jsonFactory = new JsonFactory();
    JsonParser jp = jsonFactory.createJsonParser(s);
    if (jp.nextToken() != JsonToken.START_OBJECT) {
        return null;
    }
    List<Cell> cells = new ArrayList<Cell>();
    boolean starred = false;
    boolean flagged = false;
    while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldName = jp.getCurrentName();
        jp.nextToken();
        if (STARRED.equals(fieldName)) {
            starred = jp.getBooleanValue();
        } else if (FLAGGED.equals(fieldName)) {
            flagged = jp.getBooleanValue();
        } else if ("cells".equals(fieldName)) {
            if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
                return null;
            }
            while (jp.nextToken() != JsonToken.END_ARRAY) {
                Cell cell = Cell.loadStreaming(jp, pool);
                cells.add(cell);
            }
        }
    }
    return (cells.size() > 0) ? new Row(cells, flagged, starred) : new Row(0);
}
Example 101
Project: redis-protocol-master  File: Main.java View source code
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, MustacheException {
    try {
        Args.parse(Main.class, args);
    } catch (IllegalArgumentException e) {
        Args.usage(Main.class);
        System.exit(1);
    }
    MustacheFactory mb = new DefaultMustacheFactory("templates/" + language) {

        @Override
        public void encode(String value, Writer writer) {
            try {
                writer.write(value);
            } catch (IOException e) {
                throw new MustacheException();
            }
        }
    };
    Mustache mustache = mb.compile(template + ".txt");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    XPathFactory xpf = XPathFactory.newInstance();
    final DocumentBuilder db = dbf.newDocumentBuilder();
    final XPathExpression replyX = xpf.newXPath().compile("//a");
    final Properties cache = new Properties();
    File cacheFile = new File("cache");
    if (cacheFile.exists()) {
        cache.load(new FileInputStream(cacheFile));
    }
    Set<String> ungenerated = new HashSet<String>(Arrays.asList(// Transactions
    "MULTI", // Transactions
    "EXEC", // Transactions
    "DISCARD", // subscriptions
    "PSUBSCRIBE", // subscriptions
    "SUBSCRIBE", // subscriptions
    "UNSUBSCRIBE", // subscriptions
    "PUNSUBSCRIBE", // authentication
    "AUTH"));
    final Set<String> genericReply = new HashSet<String>(Arrays.asList(// Can return an integer reply
    "SORT", // Two different return values
    "ZRANK", // Two different return values
    "ZREVRANK", // Can return a bulk or multibulk reply depending on count
    "SRANDMEMBER", // If the timeout occurs it returns a Nil multi-bulk reply instead of a bulk reply
    "BRPOPLPUSH"));
    final Set<String> multiples = new HashSet<String>(Arrays.asList("ZADD"));
    JsonFactory jf = new MappingJsonFactory();
    JsonParser jsonParser = jf.createJsonParser(new URL("https://raw.github.com/antirez/redis-doc/master/commands.json"));
    final JsonNode commandNodes = jsonParser.readValueAsTree();
    Iterator<String> fieldNames = commandNodes.getFieldNames();
    ImmutableListMultimap<String, String> group = Multimaps.index(fieldNames, new Function<String, String>() {

        @Override
        public String apply(String s) {
            return commandNodes.get(s).get("group").asText();
        }
    });
    List<Object> commands = new ArrayList<Object>();
    for (Map.Entry<String, String> entry : group.entries()) {
        String key = entry.getKey();
        final String groupName = key.substring(0, 1).toUpperCase() + key.substring(1);
        final String command = entry.getValue();
        if (ungenerated.contains(command))
            continue;
        final boolean splitCommand = command.contains(" ");
        final String safeCommand = command.replace(" ", "_");
        String cacheReply = cache.getProperty(command.toLowerCase());
        if (cacheReply == null) {
            final Document detail = db.parse("http://query.yahooapis.com/v1/public/yql/javarants/redisreply?url=" + URLEncoder.encode("http://redis.io/commands/" + safeCommand.toLowerCase(), "utf-8"));
            cacheReply = replyX.evaluate(detail).replaceAll("[- ]", "").replaceAll("reply", "Reply").replaceAll("bulk", "Bulk").replaceAll("Statuscode", "Status");
            cache.setProperty(safeCommand.toLowerCase(), cacheReply);
            cache.store(new FileWriter(cacheFile), "# Updated " + new Date());
        }
        final String finalReply = cacheReply;
        final JsonNode commandNode = commandNodes.get(command);
        commands.add(new Object() {

            String group = groupName;

            boolean split_command = splitCommand;

            String name = safeCommand;

            String name1 = splitCommand ? name.substring(0, name.indexOf("_")) : name;

            String name2 = splitCommand ? name.substring(name.indexOf("_") + 1) : "";

            String comment = commandNode.get("summary").getTextValue();

            boolean generic = finalReply.equals("") || genericReply.contains(name);

            String reply = generic ? "Reply" : finalReply;

            String version = commandNode.get("since").getTextValue();

            boolean hasOptional = false;

            boolean hasMultiple = false;

            List<String> skipped = new ArrayList<String>();

            boolean varargs() {
                return (hasMultiple || hasOptional);
            }

            boolean usearray = false;

            List<Object> arguments = new ArrayList<Object>();

            int base_length() {
                return arguments.size() - (hasMultiple ? 1 : 0);
            }

            {
                JsonNode argumentArray = commandNode.get("arguments");
                if (argumentArray != null) {
                    if (argumentArray.size() > 3) {
                        usearray = true;
                    }
                    boolean first = true;
                    int argNum = 0;
                    if (multiples.contains(name)) {
                        arguments.add(new Object() {

                            boolean first = true;

                            boolean multiple = true;

                            String typename = "Object";

                            String name = "args";
                        });
                    } else {
                        for (final JsonNode argumentNode : argumentArray) {
                            JsonNode nameNodes = argumentNode.get("name");
                            final String argName;
                            if (nameNodes.isArray()) {
                                boolean f = true;
                                StringBuilder sb = new StringBuilder();
                                for (JsonNode nameNode : nameNodes) {
                                    if (!f) {
                                        sb.append("_or_");
                                    }
                                    f = false;
                                    String textValue = nameNode.getTextValue();
                                    sb.append(textValue);
                                }
                                argName = sb.toString();
                            } else {
                                argName = nameNodes.getTextValue();
                            }
                            final boolean finalFirst = first;
                            final int finalArgNum = argNum;
                            final boolean isMultiple = argumentNode.get("multiple") != null || argumentNode.get("command") != null;
                            final boolean isOptional = argumentNode.get("optional") != null;
                            if (isOptional)
                                hasOptional = true;
                            if (isMultiple)
                                hasMultiple = true;
                            final int finalArgNum1 = argNum;
                            arguments.add(new Object() {

                                int arg_num = finalArgNum1;

                                boolean first = finalFirst;

                                boolean multiple = isMultiple;

                                String typename = "Object";

                                String name() {
                                    String name = (argName + finalArgNum).replaceAll("[- :]", "_");
                                    for (String s : skipped) {
                                        name = s + "_" + name;
                                    }
                                    return name;
                                }

                                Boolean optional = isOptional;

                                boolean skip() {
                                    boolean skip = hasMultiple && isOptional && !isMultiple;
                                    if (skip) {
                                        skipped.add(argName);
                                    }
                                    return skip;
                                }
                            });
                            if (isMultiple) {
                                usearray = true;
                            }
                            first = false;
                            argNum++;
                            if (isMultiple)
                                break;
                        }
                    }
                }
            }

            String methodname = safeCommand.toLowerCase();

            String quote = keywords.contains(methodname) ? "`" : "";
        });
    }
    Map<String, Object> ctx = new HashMap<String, Object>();
    ctx.put("commands", commands);
    File base = new File(dest, pkg.replace(".", "/"));
    base.mkdirs();
    mustache.execute(new FileWriter(new File(base, className + "." + language)), ctx).close();
}