Search in sources :

Example 41 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project jstorm by alibaba.

the class JSONUtil method formatPOJO2JSON.

/**
	 * POJO对象转换为JSON
	 * @param pojo
	 * @return
	 */
public String formatPOJO2JSON(Object pojo) {
    StringWriter stringWriter = new StringWriter();
    String json = "";
    try {
        JsonGenerator gen = new JsonFactory().createJsonGenerator(stringWriter);
        MAPPER.writeValue(gen, pojo);
        gen.close();
        json = stringWriter.toString();
    } catch (Exception e) {
        LOGGER.error(pojo.getClass().getName() + "转json出错", e);
    }
    return json;
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(org.codehaus.jackson.JsonFactory) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 42 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project voldemort by voldemort.

the class VoldemortAdminTool method executeFetchKeys.

private static void executeFetchKeys(Integer nodeId, AdminClient adminClient, List<Integer> partitionIdList, String outputDir, List<String> storeNames, boolean useAscii, boolean fetchOrphaned) throws IOException {
    List<StoreDefinition> storeDefinitionList = getStoreDefinitions(adminClient, nodeId);
    Map<String, StoreDefinition> storeDefinitionMap = Maps.newHashMap();
    for (StoreDefinition storeDefinition : storeDefinitionList) {
        storeDefinitionMap.put(storeDefinition.getName(), storeDefinition);
    }
    File directory = null;
    if (outputDir != null) {
        directory = new File(outputDir);
        if (!(directory.exists() || directory.mkdir())) {
            Utils.croak("Can't find or create directory " + outputDir);
        }
    }
    List<String> stores = storeNames;
    if (stores == null) {
        stores = Lists.newArrayList();
        stores.addAll(storeDefinitionMap.keySet());
    } else {
        // add system stores to the map so they can be fetched when
        // specified explicitly
        storeDefinitionMap.putAll(getSystemStoreDefs());
    }
    // Pick up all the partitions
    if (partitionIdList == null) {
        partitionIdList = Lists.newArrayList();
        for (Node node : adminClient.getAdminClientCluster().getNodes()) {
            partitionIdList.addAll(node.getPartitionIds());
        }
    }
    StoreDefinition storeDefinition = null;
    for (String store : stores) {
        storeDefinition = storeDefinitionMap.get(store);
        if (null == storeDefinition) {
            System.out.println("No store found under the name \'" + store + "\'");
            continue;
        }
        Iterator<ByteArray> keyIteratorRef = null;
        if (fetchOrphaned) {
            System.out.println("Fetching orphaned keys  of " + store);
            keyIteratorRef = adminClient.bulkFetchOps.fetchOrphanedKeys(nodeId, store);
        } else {
            System.out.println("Fetching keys in partitions " + Joiner.on(", ").join(partitionIdList) + " of " + store);
            keyIteratorRef = adminClient.bulkFetchOps.fetchKeys(nodeId, store, partitionIdList, null, false);
        }
        File outputFile = null;
        if (directory != null) {
            outputFile = new File(directory, store + ".keys");
        }
        final Iterator<ByteArray> keyIterator = keyIteratorRef;
        if (useAscii) {
            final SerializerDefinition serializerDef = storeDefinition.getKeySerializer();
            final SerializerFactory serializerFactory = new DefaultSerializerFactory();
            @SuppressWarnings("unchecked") final Serializer<Object> serializer = (Serializer<Object>) serializerFactory.getSerializer(serializerDef);
            final CompressionStrategy keysCompressionStrategy;
            if (serializerDef != null && serializerDef.hasCompression()) {
                keysCompressionStrategy = new CompressionStrategyFactory().get(serializerDef.getCompression());
            } else {
                keysCompressionStrategy = null;
            }
            writeAscii(outputFile, new Writable() {

                @Override
                public void writeTo(BufferedWriter out) throws IOException {
                    while (keyIterator.hasNext()) {
                        final JsonGenerator generator = new JsonFactory(new ObjectMapper()).createJsonGenerator(out);
                        byte[] keyBytes = keyIterator.next().get();
                        Object keyObject = serializer.toObject((null == keysCompressionStrategy) ? keyBytes : keysCompressionStrategy.inflate(keyBytes));
                        if (keyObject instanceof GenericRecord) {
                            out.write(keyObject.toString());
                        } else {
                            generator.writeObject(keyObject);
                        }
                        out.write('\n');
                    }
                }
            });
        } else {
            writeBinary(outputFile, new Printable() {

                @Override
                public void printTo(DataOutputStream out) throws IOException {
                    while (keyIterator.hasNext()) {
                        byte[] keyBytes = keyIterator.next().get();
                        out.writeChars(ByteUtils.toHexString(keyBytes) + "\n");
                    }
                }
            });
        }
        if (outputFile != null)
            System.out.println("Fetched keys from " + store + " to " + outputFile);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) Node(voldemort.cluster.Node) JsonFactory(org.codehaus.jackson.JsonFactory) CompressionStrategy(voldemort.store.compress.CompressionStrategy) CompressionStrategyFactory(voldemort.store.compress.CompressionStrategyFactory) BufferedWriter(java.io.BufferedWriter) StoreDefinition(voldemort.store.StoreDefinition) ByteArray(voldemort.utils.ByteArray) JsonGenerator(org.codehaus.jackson.JsonGenerator) GenericRecord(org.apache.avro.generic.GenericRecord) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Serializer(voldemort.serialization.Serializer) StringSerializer(voldemort.serialization.StringSerializer) DefaultSerializerFactory(voldemort.serialization.DefaultSerializerFactory) SerializerFactory(voldemort.serialization.SerializerFactory) IOException(java.io.IOException) DefaultSerializerFactory(voldemort.serialization.DefaultSerializerFactory) File(java.io.File) SerializerDefinition(voldemort.serialization.SerializerDefinition)

Example 43 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project voldemort by voldemort.

the class VoldemortAdminTool method executeFetchEntries.

private static void executeFetchEntries(Integer nodeId, AdminClient adminClient, List<Integer> partitionIdList, String outputDir, List<String> storeNames, boolean useAscii, boolean fetchOrphaned) throws IOException {
    List<StoreDefinition> storeDefinitionList = getStoreDefinitions(adminClient, nodeId);
    HashMap<String, StoreDefinition> storeDefinitionMap = Maps.newHashMap();
    for (StoreDefinition storeDefinition : storeDefinitionList) {
        storeDefinitionMap.put(storeDefinition.getName(), storeDefinition);
    }
    File directory = null;
    if (outputDir != null) {
        directory = new File(outputDir);
        if (!(directory.exists() || directory.mkdir())) {
            Utils.croak("Can't find or create directory " + outputDir);
        }
    }
    List<String> stores = storeNames;
    if (stores == null) {
        // when no stores specified, all user defined store will be fetched,
        // but not system stores.
        stores = Lists.newArrayList();
        stores.addAll(storeDefinitionMap.keySet());
    } else {
        // add system stores to the map so they can be fetched when
        // specified explicitly
        storeDefinitionMap.putAll(getSystemStoreDefs());
    }
    // Pick up all the partitions
    if (partitionIdList == null) {
        partitionIdList = Lists.newArrayList();
        for (Node node : adminClient.getAdminClientCluster().getNodes()) {
            partitionIdList.addAll(node.getPartitionIds());
        }
    }
    StoreDefinition storeDefinition = null;
    for (String store : stores) {
        storeDefinition = storeDefinitionMap.get(store);
        if (null == storeDefinition) {
            System.out.println("No store found under the name \'" + store + "\'");
            continue;
        }
        Iterator<Pair<ByteArray, Versioned<byte[]>>> entriesIteratorRef = null;
        if (fetchOrphaned) {
            System.out.println("Fetching orphaned entries of " + store);
            entriesIteratorRef = adminClient.bulkFetchOps.fetchOrphanedEntries(nodeId, store);
        } else {
            System.out.println("Fetching entries in partitions " + Joiner.on(", ").join(partitionIdList) + " of " + store);
            entriesIteratorRef = adminClient.bulkFetchOps.fetchEntries(nodeId, store, partitionIdList, null, false);
        }
        final Iterator<Pair<ByteArray, Versioned<byte[]>>> entriesIterator = entriesIteratorRef;
        File outputFile = null;
        if (directory != null) {
            outputFile = new File(directory, store + ".entries");
        }
        if (useAscii) {
            // k-v serializer
            SerializerDefinition keySerializerDef = storeDefinition.getKeySerializer();
            SerializerDefinition valueSerializerDef = storeDefinition.getValueSerializer();
            SerializerFactory serializerFactory = new DefaultSerializerFactory();
            @SuppressWarnings("unchecked") final Serializer<Object> keySerializer = (Serializer<Object>) serializerFactory.getSerializer(keySerializerDef);
            @SuppressWarnings("unchecked") final Serializer<Object> valueSerializer = (Serializer<Object>) serializerFactory.getSerializer(valueSerializerDef);
            // compression strategy
            final CompressionStrategy keyCompressionStrategy;
            final CompressionStrategy valueCompressionStrategy;
            if (keySerializerDef != null && keySerializerDef.hasCompression()) {
                keyCompressionStrategy = new CompressionStrategyFactory().get(keySerializerDef.getCompression());
            } else {
                keyCompressionStrategy = null;
            }
            if (valueSerializerDef != null && valueSerializerDef.hasCompression()) {
                valueCompressionStrategy = new CompressionStrategyFactory().get(valueSerializerDef.getCompression());
            } else {
                valueCompressionStrategy = null;
            }
            writeAscii(outputFile, new Writable() {

                @Override
                public void writeTo(BufferedWriter out) throws IOException {
                    while (entriesIterator.hasNext()) {
                        final JsonGenerator generator = new JsonFactory(new ObjectMapper()).createJsonGenerator(out);
                        Pair<ByteArray, Versioned<byte[]>> kvPair = entriesIterator.next();
                        byte[] keyBytes = kvPair.getFirst().get();
                        byte[] valueBytes = kvPair.getSecond().getValue();
                        VectorClock version = (VectorClock) kvPair.getSecond().getVersion();
                        Object keyObject = keySerializer.toObject((null == keyCompressionStrategy) ? keyBytes : keyCompressionStrategy.inflate(keyBytes));
                        Object valueObject = valueSerializer.toObject((null == valueCompressionStrategy) ? valueBytes : valueCompressionStrategy.inflate(valueBytes));
                        if (keyObject instanceof GenericRecord) {
                            out.write(keyObject.toString());
                        } else {
                            generator.writeObject(keyObject);
                        }
                        out.write(' ' + version.toString() + ' ');
                        if (valueObject instanceof GenericRecord) {
                            out.write(valueObject.toString());
                        } else {
                            generator.writeObject(valueObject);
                        }
                        out.write('\n');
                    }
                }
            });
        } else {
            writeBinary(outputFile, new Printable() {

                @Override
                public void printTo(DataOutputStream out) throws IOException {
                    while (entriesIterator.hasNext()) {
                        Pair<ByteArray, Versioned<byte[]>> kvPair = entriesIterator.next();
                        byte[] keyBytes = kvPair.getFirst().get();
                        VectorClock clock = ((VectorClock) kvPair.getSecond().getVersion());
                        byte[] valueBytes = kvPair.getSecond().getValue();
                        out.writeChars(ByteUtils.toHexString(keyBytes));
                        out.writeChars(",");
                        out.writeChars(clock.toString());
                        out.writeChars(",");
                        out.writeChars(ByteUtils.toHexString(valueBytes));
                        out.writeChars("\n");
                    }
                }
            });
        }
        if (outputFile != null)
            System.out.println("Fetched keys from " + store + " to " + outputFile);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) Node(voldemort.cluster.Node) JsonFactory(org.codehaus.jackson.JsonFactory) CompressionStrategy(voldemort.store.compress.CompressionStrategy) CompressionStrategyFactory(voldemort.store.compress.CompressionStrategyFactory) BufferedWriter(java.io.BufferedWriter) StoreDefinition(voldemort.store.StoreDefinition) JsonGenerator(org.codehaus.jackson.JsonGenerator) GenericRecord(org.apache.avro.generic.GenericRecord) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Pair(voldemort.utils.Pair) Serializer(voldemort.serialization.Serializer) StringSerializer(voldemort.serialization.StringSerializer) DefaultSerializerFactory(voldemort.serialization.DefaultSerializerFactory) SerializerFactory(voldemort.serialization.SerializerFactory) VectorClock(voldemort.versioning.VectorClock) IOException(java.io.IOException) DefaultSerializerFactory(voldemort.serialization.DefaultSerializerFactory) File(java.io.File) SerializerDefinition(voldemort.serialization.SerializerDefinition)

Example 44 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project spring-data-document-examples by spring-projects.

the class MappingJacksonHttpMessageConverter method writeInternal.

@Override
protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    JsonEncoding encoding = getEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, o);
    } catch (JsonGenerationException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(org.codehaus.jackson.JsonEncoding) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 45 with JsonGenerator

use of org.codehaus.jackson.JsonGenerator in project SimianArmy by Netflix.

the class ChaosMonkeyResource method addEvent.

/**
     * POST /api/v1/chaos will try a add a new event with the information in the url context,
     * ignoring the monkey probability and max termination configurations, for a specific instance group.
     *
     * @param content
     *            the Json content passed to the http POST request
     * @return the response
     * @throws IOException
     */
@POST
public Response addEvent(String content) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    LOGGER.info(String.format("JSON content: '%s'", content));
    JsonNode input = mapper.readTree(content);
    String eventType = getStringField(input, "eventType");
    String groupType = getStringField(input, "groupType");
    String groupName = getStringField(input, "groupName");
    String chaosTypeName = getStringField(input, "chaosType");
    ChaosType chaosType;
    if (!Strings.isNullOrEmpty(chaosTypeName)) {
        chaosType = ChaosType.parse(this.monkey.getChaosTypes(), chaosTypeName);
    } else {
        chaosType = new ShutdownInstanceChaosType(monkey.context().configuration());
    }
    Response.Status responseStatus;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonGenerator gen = JSON_FACTORY.createJsonGenerator(baos, JsonEncoding.UTF8);
    gen.writeStartObject();
    gen.writeStringField("eventType", eventType);
    gen.writeStringField("groupType", groupType);
    gen.writeStringField("groupName", groupName);
    gen.writeStringField("chaosType", chaosType.getKey());
    if (StringUtils.isEmpty(eventType) || StringUtils.isEmpty(groupType) || StringUtils.isEmpty(groupName)) {
        responseStatus = Response.Status.BAD_REQUEST;
        gen.writeStringField("message", "eventType, groupType, and groupName parameters are all required");
    } else {
        if (eventType.equals("CHAOS_TERMINATION")) {
            responseStatus = addTerminationEvent(groupType, groupName, chaosType, gen);
        } else {
            responseStatus = Response.Status.BAD_REQUEST;
            gen.writeStringField("message", String.format("Unrecognized event type: %s", eventType));
        }
    }
    gen.writeEndObject();
    gen.close();
    LOGGER.info("entity content is '{}'", baos.toString("UTF-8"));
    return Response.status(responseStatus).entity(baos.toString("UTF-8")).build();
}
Also used : Response(javax.ws.rs.core.Response) ShutdownInstanceChaosType(com.netflix.simianarmy.chaos.ShutdownInstanceChaosType) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonNode(org.codehaus.jackson.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ChaosType(com.netflix.simianarmy.chaos.ChaosType) ShutdownInstanceChaosType(com.netflix.simianarmy.chaos.ShutdownInstanceChaosType) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) POST(javax.ws.rs.POST)

Aggregations

JsonGenerator (org.codehaus.jackson.JsonGenerator)54 JsonFactory (org.codehaus.jackson.JsonFactory)15 IOException (java.io.IOException)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)11 StringWriter (java.io.StringWriter)10 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)8 RpcException (cz.metacentrum.perun.core.api.exceptions.RpcException)6 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)5 JsonNode (org.codehaus.jackson.JsonNode)5 OutputStreamWriter (java.io.OutputStreamWriter)4 HashMap (java.util.HashMap)4 File (java.io.File)3 PrintWriter (java.io.PrintWriter)3 GET (javax.ws.rs.GET)3 Response (javax.ws.rs.core.Response)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 BufferedWriter (java.io.BufferedWriter)2 DataOutputStream (java.io.DataOutputStream)2 ArrayList (java.util.ArrayList)2