Package javax.json

Examples of javax.json.JsonObjectBuilder


  @Override
  public String encode(AsciidocMessage m) throws EncodeException {
    StringWriter swriter = new StringWriter();
    try (JsonWriter jsonWrite = Json.createWriter(swriter)) {
      JsonObjectBuilder builder = Json.createObjectBuilder();
      builder.add("type", m.getType().toString())
          .add("adocId", m.getAdocId())
          .add("data",
              Json.createObjectBuilder()
                  .add("format", m.getFormat().toString())
                  .add("source", m.getAdocSource())
                  .add("sourceToMerge", m.getAdocSourceToMerge()!=null?m.getAdocSourceToMerge():""));

      jsonWrite.writeObject(builder.build());
    }
    return swriter.toString();
  }
View Full Code Here


  @Override
  public String encode(OutputMessage m) {
    StringWriter swriter = new StringWriter();
    try {
      try (JsonWriter jsonWrite = Json.createWriter(swriter)) {
        JsonObjectBuilder builder = Json.createObjectBuilder();
       
        JsonObjectBuilder dataBuilder = Json.createObjectBuilder();
        dataBuilder.add("format", m.getFormat().toString()).add("currentWriter",
            m.getCurrentWriter()).add("source", m.getAdocSource()).add("timeToRender",
            m.getTimeToRender()).add("output", m.getContent());
        //addHeader
        if (m.getDocHeader() != null && m.getDocHeader()
            .getDocumentTitle() != null && m.getDocHeader().getAuthor() != null
            && m.getDocHeader().getRevisionInfo().getNumber() != null){
          dataBuilder.add("docHeader",
              Json.createObjectBuilder()
                  .add("title",
                      m.getDocHeader()
                          .getDocumentTitle())
                  .add("author", m.getDocHeader().getAuthor().getFullName())
                  .add("revisioninfo",
                      m.getDocHeader()
                          .getRevisionInfo()
                          .getNumber()));
        }
       
        builder.add("type", m.getType().toString())
            .add("adocId", m.getAdocId())
            .add("data", dataBuilder.build());
       
        jsonWrite.writeObject(builder.build());
      }
    } catch (Exception e) {
      logger.severe("Output message decode error.");
View Full Code Here

    return swriter.toString();
  }

  private JsonObjectBuilder getDefault(OutputMessage m) {

    JsonObjectBuilder builder = Json.createObjectBuilder();
    builder.add("type", m.getType().toString())
        .add("adocId", m.getAdocId())
        .add("data",
            Json.createObjectBuilder()
                .add("format", "")
                .add("currentWriter", "")
View Full Code Here

        books.clear();
        return Response.ok().build();
    }

    private JsonObject bookToJson(final Book book) {
        final JsonObjectBuilder builder = Json
                .createObjectBuilder()
                .add("id", book.getId())
                .add("name", book.getName());
       
        if (!book.getChapters().isEmpty()) {
            final JsonArrayBuilder chapters = Json.createArrayBuilder();
           
            for (final BookChapter chapter: book.getChapters()) {
                chapters.add(Json.createObjectBuilder()
                    .add("id", chapter.getId())
                    .add("title", chapter.getTitle())
                );
            }
           
            builder.add("chapters", chapters);
        }
               
        return builder.build();
    }
View Full Code Here

     */

    protected JsonValue _deserializeObject(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException
    {
        JsonObjectBuilder b = _builderFactory.createObjectBuilder();
        while (jp.nextToken() != JsonToken.END_OBJECT) {
            String name = jp.getCurrentName();
            JsonToken t = jp.nextToken();
            switch (t) {
            case START_ARRAY:
                b.add(name, _deserializeArray(jp, ctxt));
                break;
            case START_OBJECT:
                b.add(name, _deserializeObject(jp, ctxt));
                break;
            case VALUE_FALSE:
                b.add(name, false);
                break;
            case VALUE_TRUE:
                b.add(name, true);
                break;
            case VALUE_NULL:
                b.addNull(name);
                break;
            case VALUE_NUMBER_FLOAT:
                if (jp.getNumberType() == NumberType.BIG_DECIMAL) {
                    b.add(name, jp.getDecimalValue());
                } else {
                    b.add(name, jp.getDoubleValue());
                }
                break;
            case VALUE_NUMBER_INT:
                // very cumbersome... but has to be done
                switch (jp.getNumberType()) {
                case LONG:
                    b.add(name, jp.getLongValue());
                    break;
                case INT:
                    b.add(name, jp.getIntValue());
                    break;
                default:
                    b.add(name, jp.getBigIntegerValue());
                }
                break;
            case VALUE_STRING:
                b.add(name, jp.getText());
                break;
            default:
                throw ctxt.mappingException(JsonValue.class);
            }
        }
        return b.build();
    }
View Full Code Here

    protected void writeEmptyCollection(Level level, String keyName){
        level.getJsonObjectBuilder().add(keyName, Json.createArrayBuilder());
    }
     
    protected void addValueToObject(Level level, String keyName, Object value, QName schemaType){
        JsonObjectBuilder jsonObjectBuilder = level.getJsonObjectBuilder();
        if(value == NULL){
            jsonObjectBuilder.addNull(keyName);
        }else if(value instanceof Integer){
            jsonObjectBuilder.add(keyName, (Integer)value)
        }else if(value instanceof BigDecimal){
            jsonObjectBuilder.add(keyName, (BigDecimal)value);  
        }else if(value instanceof BigInteger){
            jsonObjectBuilder.add(keyName, (BigInteger)value);              
        }else if(value instanceof Boolean){
            jsonObjectBuilder.add(keyName, (Boolean)value);
        }else if(value instanceof Character){
            jsonObjectBuilder.add(keyName, (Character)value)
        }else if(value instanceof Double){
            jsonObjectBuilder.add(keyName, (Double)value);
        }else if(value instanceof Float){
            jsonObjectBuilder.add(keyName, (Float)value);
        }else if(value instanceof Long){
            jsonObjectBuilder.add(keyName, (Long)value);
        }else if(value instanceof String){
            jsonObjectBuilder.add(keyName, (String)value);               
        }else{
            ConversionManager conversionManager = getConversionManager();
            String convertedValue = (String) conversionManager.convertObject(value, CoreClassConstants.STRING, schemaType);
            Class theClass = conversionManager.javaType(schemaType);
            if((schemaType == null || theClass == null) && (CoreClassConstants.NUMBER.isAssignableFrom(value.getClass()))){
                //if it's still a number and falls through the cracks we dont want "" around the value
                    BigDecimal convertedNumberValue = ((BigDecimal) ((ConversionManager) session.getDatasourcePlatform().getConversionManager()).convertObject(value, CoreClassConstants.BIGDECIMAL, schemaType));
                    jsonObjectBuilder.add(keyName, (BigDecimal)convertedNumberValue);
            }else{
                jsonObjectBuilder.add(keyName, convertedValue);
            }
               
        }
    }
View Full Code Here

   * Serialize the metadata as a JSON-encoded string
   *
   * @return
   */
  public String jsonSerialize() {
    JsonObjectBuilder d = Json.createObjectBuilder();
    for (String key:this) {
      d.add(key, get(key));
    }
    return d.build().toString();
  }
View Full Code Here

    public String write(boolean pretty) {
        StringWriter stringWriter = new StringWriter();
        Map<String, Object> config = new HashMap<>(1);
        config.put(JsonGenerator.PRETTY_PRINTING, pretty);
        try (JsonWriter writer = Json.createWriterFactory(config).createWriter(stringWriter)) {
            JsonObjectBuilder object = Json.createObjectBuilder()
                    .add("type", "FeatureCollection")
                    .add("generator", "JOSM");
            appendLayerBounds(layer.data, object);
            appendLayerFeatures(layer.data, object);
            writer.writeObject(object.build());
            return stringWriter.toString();
        }
    }
View Full Code Here

        } else if (skipEmptyNodes && p instanceof Node && p.getKeys().isEmpty()) {
            return;
        }

        // Properties
        final JsonObjectBuilder propObj = Json.createObjectBuilder();
        for (Entry<String, String> t : p.getKeys().entrySet()) {
            propObj.add(t.getKey(), t.getValue());
        }

        // Geometry
        final JsonObjectBuilder geomObj = Json.createObjectBuilder();
        p.accept(new GeometryPrimitiveVisitor(geomObj));

        // Build primitive JSON object
        array.add(Json.createObjectBuilder()
                .add("type", "Feature")
View Full Code Here

   * @throws SimulationContextException
   */
  private JsonObject buildContextUpdate() throws SimulationContextException {
    ISimulationController controller = server.getController();
    List<Room> rooms = controller.getRooms();
    JsonObjectBuilder contextUpdateBuilder = Json.createObjectBuilder();
    if (rooms != null) {
      for (Room room : rooms) {
        String name = room.getName();
        if (name.equalsIgnoreCase(roomId)) {
          SimulationContext context = room.getLocalContext();
          int[] lightColor = context.getLightColor();
          contextUpdateBuilder
              .add(SimulationContext.BRIGHTNESS,
                  context.getBrightness())
              .add(SimulationContext.TEMPERATURE,
                  context.getTemperature())
              .add(SimulationContext.LIGHTCOLOR,
                  String.format("%d %d %d", lightColor[0],
                      lightColor[1], lightColor[2]))
              .add(SimulationContext.ENERGY_CONSUMPTION,
                  context.getEnergyConsumption())
              .add(SimulationContext.MUSIC_GENRE,
                  context.getMusicGenre())
              .add(SimulationContext.MUSIC_VOLUME,
                  context.getMusicVolume());
        }
      }
    }
    JsonObject contextUpdate = contextUpdateBuilder.build();
    return contextUpdate;
  }
View Full Code Here

TOP

Related Classes of javax.json.JsonObjectBuilder

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.