Example usage for com.fasterxml.jackson.core JsonEncoding UTF8

List of usage examples for com.fasterxml.jackson.core JsonEncoding UTF8

Introduction

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

Prototype

JsonEncoding UTF8

To view the source code for com.fasterxml.jackson.core JsonEncoding UTF8.

Click Source Link

Usage

From source file:com.rhfung.P2PDictionary.DataConnection.java

private byte[] GetDictionaryAsJson() {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory 
    JsonGenerator jg;//from  w  w  w .j  av a2 s .c  o  m
    try {
        jg = jsonFactory.createJsonGenerator(stream, JsonEncoding.UTF8);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return new byte[0];
    } // or Stream, Reader

    List<DataEntry> entries; // make a local copy for access without worry about changes thereafter
    this.dataLock.readLock().lock();
    try {
        entries = new Vector<DataEntry>(this.data.size());
        //entries.AddRange(this.data.Values.Where(x => x.subscribed));
        entries.addAll(this.data.values());
    } finally {
        this.dataLock.readLock().unlock();
    }

    // write count of data entries
    //writer.write(DATA_NAMESPACE + "/\t" + this.local_uid + "\t0\tRW\t" + entries.size() + "\t" + this.local_uid + NEWLINE) ;
    try {
        jg.writeStartObject();
        jg.writeObjectField("size", entries.size());
        jg.writeObjectField("localid", this.local_uid);

        jg.writeArrayFieldStart("keys");

        // write each data entry, converting simple data immediately
        // (pretend i don't know about these non-subscribed entries)
        for (DataEntry d : entries) {
            WriteJSONForEntry(jg, d);
        }

        jg.writeEndArray();
        jg.writeEndObject();

        jg.close();
    } catch (JsonGenerationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return stream.toByteArray();
}