Package com.fasterxml.jackson.core

Examples of com.fasterxml.jackson.core.JsonGenerator


    }

    @Override
    public void render(PropertyState property, HttpServletResponse response)
            throws IOException {
        JsonGenerator generator = startResponse(response);
        render(property, generator);
        generator.close();
    }
View Full Code Here


      ServletOutputStream outputStream = response.getOutputStream();

      if (!streamResponse) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(bos, JsonEncoding.UTF8);
        objectMapper.writeValue(jsonGenerator, responseObject);
        response.setContentLength(bos.size());
        outputStream.write(bos.toByteArray());
        jsonGenerator.close();
      } else {
        JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(outputStream,
            JsonEncoding.UTF8);
        objectMapper.writeValue(jsonGenerator, responseObject);
        jsonGenerator.close();
      }

      outputStream.flush();
    }
  }
View Full Code Here

   * @version 1.0.0
   *
   */
  public static String beanToJson(Object o) throws Exception {
    StringWriter writer = null;
    JsonGenerator gen = null;
    try {
      writer = new StringWriter();
      gen = objectMapper.getJsonFactory().createJsonGenerator(writer);
      objectMapper.writeValue(gen, o);

      return writer.toString();
    } catch (JsonGenerationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        if (gen != null) {
          gen.close();
        }
        if (writer != null) {
          writer.close();
        }
      } catch (IOException e) {
View Full Code Here

    testDao.add(test);
    // 测试回滚
    // test.setUsername("testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestttesttesttesttesttest");
    // testDao.add(test);
    ObjectMapper mapper = new ObjectMapper();
    JsonGenerator jsonGenerator = mapper.getJsonFactory().createJsonGenerator(System.out, JsonEncoding.UTF8);
    // mapper.getJsonFactory().createjsong

    jsonGenerator.writeObject(test);
    System.out.println();
    mapper.writeValue(System.out, test);

    return null;
  }
View Full Code Here

      @SuppressWarnings("resource")
      ServletOutputStream outputStream = response.getOutputStream();

      if (!streamResponse) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(bos, JsonEncoding.UTF8);
        objectMapper.writeValue(jsonGenerator, responseObject);
        response.setContentLength(bos.size());
        outputStream.write(bos.toByteArray());
        jsonGenerator.close();
      } else {
        JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(outputStream,
            JsonEncoding.UTF8);
        objectMapper.writeValue(jsonGenerator, responseObject);
        jsonGenerator.close();
      }

      outputStream.flush();
    }
  }
View Full Code Here

    Class en = CONFIGURATION_SECTIONS.get(section);
    try {
      JsonFactory jfactory = new JsonFactory();
      StringWriter sw = new StringWriter();
      String enumDescription = "";     
      JsonGenerator gen = jfactory.createJsonGenerator(sw);
     
      Method getEnumDescription= en.getMethod("getEnumDescription");
      if (getEnumDescription!=null  && getEnumDescription.getReturnType()==String.class && Modifier.isStatic(getEnumDescription.getModifiers()))
          enumDescription=(String) getEnumDescription.invoke(null);
      gen.writeStartObject();                                            //{
      gen.writeStringField("section", section);                          //   "configuration":"EnumName"
      gen.writeStringField("description", enumDescription);                            //  ,"description": "EnumDescription"
      gen.writeFieldName("sub sections");                                        //  ,"sections":
      gen.writeStartObject();                                            //    {
      String lastSection = "";
      EnumSet values = EnumSet.allOf( en );
      for (Object v : values) {
          String key=(String) (en.getMethod("getKey")).invoke(v);
          boolean isVisible=(Boolean)(en.getMethod("isVisible")).invoke(v);
          String valueAsString;
          if (isVisible) valueAsString=(String) (en.getMethod("getValueAsString")).invoke(v);
          else valueAsString = "--HIDDEN--";
          boolean isEditable=(Boolean)(en.getMethod("isEditable")).invoke(v);
          String valueDescription=(String) (en.getMethod("getValueDescription")).invoke(v);
          Class type = (Class) en.getMethod("getType").invoke(v);
            String subsection = key.substring(0, key.indexOf('.'));
            if (!lastSection.equals(subsection)) {
            if (gen.getOutputContext().inArray()) gen.writeEndArray();
              gen.writeFieldName(subsection);                                    //      "sectionName":
              gen.writeStartArray();                                        //        [
              lastSection = subsection;
           
            boolean isOverridden = (Boolean)(en.getMethod("isOverridden")).invoke(v);
            gen.writeStartObject();                                        //          {
            gen.writeStringField(key,valueAsString);                              //              "key": "value" 
            gen.writeStringField("description", valueDescription);                        //            ,"description":"description"
            gen.writeStringField("type",type.getSimpleName());                          //            ,"type":"type"
            gen.writeBooleanField("editable",isEditable);                          //            ,"editable":"true|false"
            gen.writeBooleanField("visible",isVisible);                          //            ,"visible":"true|false"
            gen.writeBooleanField("overridden",isOverridden);                          //            ,"overridden":"true|false"
            gen.writeEndObject();                                          //          }
      }
      if (gen.getOutputContext().inArray()) gen.writeEndArray();                          //        ]
      gen.writeEndObject();                                            //    }
      gen.writeEndObject();                                          //}
      gen.close();
      return sw.toString();
    } catch (Exception e) {
      Logger.error("Cannot generate a json for "+ en.getSimpleName()+" Enum. Is it an Enum that implements the IProperties interface?",e);
    }
    return "{}";
View Full Code Here

    ImmutableCollection<String> keys = CONFIGURATION_SECTIONS.keySet()
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory jfactory = mapper.getJsonFactory();
    StringWriter sw = new StringWriter()
    try{
      JsonGenerator gen = jfactory.createJsonGenerator(sw);
      gen.writeStartArray()
      for (String v: keys){
        String st = dumpConfigurationAsJson(v);
        ObjectMapper op= new ObjectMapper();
        JsonNode p = op.readTree(st);
        Logger.debug("OBJECT:" + p.toString());
        Logger.debug("STRING:" + st);
        //JsonParser jp = jfactory.createJsonParser(st);
        gen.writeTree(p);
      }
      gen.writeEndArray();
      gen.close();
      return sw.toString();
    }catch (Exception e) {
      Logger.error("Cannot generate a json for the configuration",e);
    }
    return "[]";
View Full Code Here

    Class en = CONFIGURATION_SECTIONS.get(section);
    try {
      JsonFactory jfactory = new JsonFactory();
      StringWriter sw = new StringWriter();
      String enumDescription = "";     
      JsonGenerator gen = jfactory.createJsonGenerator(sw);
      gen.writeStartArray()
      EnumSet values = EnumSet.allOf( en );
      for (Object v : values) {
          String key=(String) (en.getMethod("getKey")).invoke(v);
         
         
          boolean isVisible=(Boolean)(en.getMethod("isVisible")).invoke(v);
          String valueAsString;
          if (isVisible) valueAsString=(String) (en.getMethod("getValueAsString")).invoke(v);
          else valueAsString = "--HIDDEN--";
          boolean isEditable=(Boolean)(en.getMethod("isEditable")).invoke(v);
            boolean isOverridden = (Boolean)(en.getMethod("isOverridden")).invoke(v);
          String valueDescription=(String) (en.getMethod("getValueDescription")).invoke(v);
          Class type = (Class) en.getMethod("getType").invoke(v);
         
            gen.writeStartObject();                                        //          {
            gen.writeStringField("key", key)
            gen.writeStringField("value",valueAsString);
            gen.writeStringField("description", valueDescription);                        //            ,"description":"description"
            gen.writeStringField("type",type.getSimpleName());                          //            ,"type":"type"
            gen.writeBooleanField("editable", isEditable);
            gen.writeBooleanField("overridden", isOverridden);
            gen.writeEndObject();                                          //          }
      }
      if (gen.getOutputContext().inArray()) gen.writeEndArray();                          //        ]
      gen.close();
      return sw.toString();
    } catch (Exception e) {
      Logger.error("Cannot generate a json for "+ en.getSimpleName()+" Enum. Is it an Enum that implements the IProperties interface?",e);
    }
    return "{}";
View Full Code Here

    try {
      // don't forget jsonp
      if (jsonp != null && !jsonp.isEmpty()) {
        output.write((jsonp + "(").getBytes(query.getCharset()));
      }
      JsonGenerator json = JSON.getFactory().createGenerator(output);
      json.writeStartArray();
     
      for (DataPoints[] separate_dps : results) {
        for (DataPoints dps : separate_dps) {
          json.writeStartObject();
         
          json.writeStringField("metric", dps.metricName());
         
          json.writeFieldName("tags");
          json.writeStartObject();
          if (dps.getTags() != null) {
            for (Map.Entry<String, String> tag : dps.getTags().entrySet()) {
              json.writeStringField(tag.getKey(), tag.getValue());
            }
          }
          json.writeEndObject();
         
          json.writeFieldName("aggregateTags");
          json.writeStartArray();
          if (dps.getAggregatedTags() != null) {
            for (String atag : dps.getAggregatedTags()) {
              json.writeString(atag);
            }
          }
          json.writeEndArray();
         
          if (data_query.getShowTSUIDs()) {
            json.writeFieldName("tsuids");
            json.writeStartArray();
            final List<String> tsuids = dps.getTSUIDs();
            Collections.sort(tsuids);
            for (String tsuid : tsuids) {
              json.writeString(tsuid);
            }
            json.writeEndArray();
          }
         
          if (!data_query.getNoAnnotations()) {
            final List<Annotation> annotations = dps.getAnnotations();
            if (annotations != null) {
              Collections.sort(annotations);
              json.writeArrayFieldStart("annotations");
              for (Annotation note : annotations) {
                json.writeObject(note);
              }
              json.writeEndArray();
            }
           
            if (globals != null && !globals.isEmpty()) {
              Collections.sort(globals);
              json.writeArrayFieldStart("globalAnnotations");
              for (Annotation note : globals) {
                json.writeObject(note);
              }
              json.writeEndArray();
            }
          }
         
          // now the fun stuff, dump the data
          json.writeFieldName("dps");
         
          // default is to write a map, otherwise write arrays
          if (as_arrays) {
            json.writeStartArray();
            for (final DataPoint dp : dps) {
              if (dp.timestamp() < data_query.startTime() ||
                  dp.timestamp() > data_query.endTime()) {
                continue;
              }
              final long timestamp = data_query.getMsResolution() ?
                  dp.timestamp() : dp.timestamp() / 1000;
              json.writeStartArray();
              json.writeNumber(timestamp);
              if (dp.isInteger()) {
                json.writeNumber(dp.longValue());
              } else {
                json.writeNumber(dp.doubleValue());
              }
              json.writeEndArray();
            }
            json.writeEndArray();
          } else {
            json.writeStartObject();
            for (final DataPoint dp : dps) {
              if (dp.timestamp() < (data_query.startTime()) ||
                  dp.timestamp() > (data_query.endTime())) {
                continue;
              }
              final long timestamp = data_query.getMsResolution() ?
                  dp.timestamp() : dp.timestamp() / 1000;
              if (dp.isInteger()) {
                json.writeNumberField(Long.toString(timestamp), dp.longValue());
              } else {
                json.writeNumberField(Long.toString(timestamp), dp.doubleValue());
              }
            }
            json.writeEndObject();
          }

          // close the results for this particular query
          json.writeEndObject();
        }
      }
   
      // close
      json.writeEndArray();
      json.close();
     
      if (jsonp != null && !jsonp.isEmpty()) {
        output.write(")".getBytes());
      }
      return response;
View Full Code Here

   */
  private byte[] getStorageJSON() {
    // 256 bytes is a good starting value, assumes default info
    final ByteArrayOutputStream output = new ByteArrayOutputStream(256);
    try {
      final JsonGenerator json = JSON.getFactory().createGenerator(output);
      json.writeStartObject();
      json.writeStringField("tsuid", tsuid);
      json.writeStringField("displayName", display_name);
      json.writeStringField("description", description);
      json.writeStringField("notes", notes);
      json.writeNumberField("created", created);
      if (custom == null) {
        json.writeNullField("custom");
      } else {
        json.writeObjectFieldStart("custom");
        for (Map.Entry<String, String> entry : custom.entrySet()) {
          json.writeStringField(entry.getKey(), entry.getValue());
        }
        json.writeEndObject();
      }
      json.writeStringField("units", units);
      json.writeStringField("dataType", data_type);
      json.writeNumberField("retention", retention);
      json.writeNumberField("max", max);
      json.writeNumberField("min", min);
     
      json.writeEndObject();
      json.close();
      return output.toByteArray();
    } catch (IOException e) {
      throw new RuntimeException("Unable to serialize TSMeta", e);
    }
  }
View Full Code Here

TOP

Related Classes of com.fasterxml.jackson.core.JsonGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.