Exemplo n.º 1
0
  public static Map<String, Object> fromJson(JsonObject json) {

    Map<String, Object> attributes = new LinkedHashMap<>();

    for (Map.Entry<String, JsonValue> entry : json.entrySet()) {
      switch (entry.getValue().getValueType()) {
        case STRING:
          attributes.put(entry.getKey(), ((JsonString) entry.getValue()).getString());
          break;
        case NUMBER:
          JsonNumber number = (JsonNumber) entry.getValue();
          if (number.isIntegral()) {
            attributes.put(entry.getKey(), number.longValue());
          } else {
            attributes.put(entry.getKey(), number.doubleValue());
          }
          break;
        case TRUE:
          attributes.put(entry.getKey(), true);
          break;
        case FALSE:
          attributes.put(entry.getKey(), false);
          break;
      }
    }

    return attributes;
  }
  private void generateFieldsAndMethods(
      final Writer writer,
      final JsonObject object,
      final String prefix,
      final Collection<String> imports)
      throws IOException {
    final Map<String, JsonObject> nestedTypes = new TreeMap<String, JsonObject>();
    {
      final Iterator<Map.Entry<String, JsonValue>> iterator = object.entrySet().iterator();
      while (iterator.hasNext()) {
        final Map.Entry<String, JsonValue> entry = iterator.next();
        final String key = entry.getKey();
        final String fieldName = toJavaFieldName(key);
        switch (entry.getValue().getValueType()) {
          case ARRAY:
            imports.add("java.util.List");
            handleArray(writer, prefix, nestedTypes, entry.getValue(), key, fieldName, 1, imports);
            break;
          case OBJECT:
            final String type = toJavaName(fieldName);
            nestedTypes.put(type, JsonObject.class.cast(entry.getValue()));
            fieldGetSetMethods(writer, key, fieldName, type, prefix, 0, imports);
            break;
          case TRUE:
          case FALSE:
            fieldGetSetMethods(writer, key, fieldName, "Boolean", prefix, 0, imports);
            break;
          case NUMBER:
            fieldGetSetMethods(writer, key, fieldName, "Double", prefix, 0, imports);
            break;
          case STRING:
            fieldGetSetMethods(writer, key, fieldName, "String", prefix, 0, imports);
            break;
          case NULL:
          default:
            throw new UnsupportedOperationException("Unsupported " + entry.getValue() + ".");
        }
        if (iterator.hasNext()) {
          writer.write("\n");
        }
      }
    }

    if (!object.isEmpty() && !nestedTypes.isEmpty()) {
      writer.write("\n");
    }

    final Iterator<Map.Entry<String, JsonObject>> entries = nestedTypes.entrySet().iterator();
    while (entries.hasNext()) {
      final Map.Entry<String, JsonObject> entry = entries.next();
      writer.write(prefix + "public static class " + entry.getKey() + " {\n");
      generateFieldsAndMethods(writer, entry.getValue(), "    " + prefix, imports);
      writer.write(prefix + "}\n");
      if (entries.hasNext()) {
        writer.write("\n");
      }
    }
  }
Exemplo n.º 3
0
 @Override
 @NotNull(message = "JSON is never NULL")
 public JsonObject json() throws IOException {
   final JsonObject obj = new JsonNode(this.storage.xml().nodes(this.xpath()).get(0)).json();
   final JsonObjectBuilder json = Json.createObjectBuilder();
   for (final Map.Entry<String, JsonValue> val : obj.entrySet()) {
     json.add(val.getKey(), val.getValue());
   }
   return json.add("comments", this.storage.xml().nodes(this.comment()).size()).build();
 }
 /**
  * Parses a JSON object into a map of links.
  *
  * @param jsonObject JSON object
  * @return map of links
  */
 public static Map<String, URL> parseLinks(JsonObject jsonObject) {
   Map<String, URL> links = new HashMap<String, URL>();
   for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
     try {
       URL link = parseLink((JsonObject) entry.getValue());
       links.put(entry.getKey(), link);
     } catch (MalformedURLException exception) {
       getLogger().log(Level.WARNING, "Could not parse a URL value", exception);
     }
   }
   return links;
 }
Exemplo n.º 5
0
 public static String toJSONText(JsonObject jsonObject) {
   StringBuilder sb = new StringBuilder(jsonObject.size() * 10);
   sb.append('{');
   Iterator<Entry<String, JsonValue>> iterator = jsonObject.entrySet().iterator();
   while (iterator.hasNext()) {
     Entry<String, JsonValue> next = iterator.next();
     sb.append(JsonString_Impl.toJSONText(next.getKey()));
     sb.append(':');
     sb.append(next.getValue());
     sb.append(iterator.hasNext() ? "," : "");
   }
   sb.append('}');
   return sb.toString();
 }
Exemplo n.º 6
0
  public SubDocument from(String subdocAsJson, SubDocType subDocType) {
    JsonReader reader = Json.createReader(new StringReader(subdocAsJson));

    JsonObject jsonObject = reader.readObject();

    SubDocument.Builder builder = new SubDocument.Builder();

    for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {
      Object objectValue = jsonValueToObject(entry.getValue());

      convertAttribute(builder, entry.getKey(), objectValue, subDocType);
    }

    return builder.build();
  }
Exemplo n.º 7
0
 public VisitingContext(JsonObject rootNode, String currentNamespace) {
   namespace = currentNamespace;
   node = rootNode;
   elements = node.entrySet().iterator();
 }