public static void dumpJSON(JsonValue tree, String key, String depthPrefix) {
   if (key != null) logger.info(depthPrefix + "Key " + key + ": ");
   switch (tree.getValueType()) {
     case OBJECT:
       logger.info(depthPrefix + "OBJECT");
       JsonObject object = (JsonObject) tree;
       for (String name : object.keySet()) dumpJSON(object.get(name), name, depthPrefix + "  ");
       break;
     case ARRAY:
       logger.info(depthPrefix + "ARRAY");
       JsonArray array = (JsonArray) tree;
       for (JsonValue val : array) dumpJSON(val, null, depthPrefix + "  ");
       break;
     case STRING:
       JsonString st = (JsonString) tree;
       logger.info(depthPrefix + "STRING " + st.getString());
       break;
     case NUMBER:
       JsonNumber num = (JsonNumber) tree;
       logger.info(depthPrefix + "NUMBER " + num.toString());
       break;
     case TRUE:
     case FALSE:
     case NULL:
       logger.info(depthPrefix + tree.getValueType().toString());
       break;
   }
 }
 @Override
 public boolean getBoolean(int index, boolean defaultValue) {
   JsonValue value = list.get(index);
   if (value == null) {
     return defaultValue;
   } else if (value.getValueType() == ValueType.TRUE) {
     return true;
   } else if (value.getValueType() == ValueType.FALSE) {
     return false;
   } else {
     // TODO check spec
     throw new IllegalStateException("index" + index + " is " + value.getValueType());
   }
 }
 private static void readJsonFile() {
   try (JsonReader jsonReader =
       Json.createReader(
           new FileReader(
               Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) {
     JsonStructure jsonStructure = jsonReader.read();
     JsonValue.ValueType valueType = jsonStructure.getValueType();
     if (valueType == JsonValue.ValueType.OBJECT) {
       JsonObject jsonObject = (JsonObject) jsonStructure;
       JsonValue firstName = jsonObject.get("firstName");
       LOGGER.info("firstName=" + firstName);
       JsonValue emailAddresses = jsonObject.get("phoneNumbers");
       if (emailAddresses.getValueType() == JsonValue.ValueType.ARRAY) {
         JsonArray jsonArray = (JsonArray) emailAddresses;
         for (int i = 0; i < jsonArray.size(); i++) {
           JsonValue jsonValue = jsonArray.get(i);
           LOGGER.info("emailAddress(" + i + "): " + jsonValue);
         }
       }
     } else {
       LOGGER.warning("First object is not of type " + JsonValue.ValueType.OBJECT);
     }
   } catch (FileNotFoundException e) {
     LOGGER.severe("Failed to open file: " + e.getMessage());
   }
 }
 /**
  * Get long value out of the message json payload
  *
  * @param key Key to find value for
  * @param defaultValue Default value if key can't be found
  * @return value in object, or the provided default
  */
 public long getLong(String key, long defaultValue) {
   JsonObject obj = getParsedBody();
   JsonValue value = obj.get(key);
   if (value != null && value.getValueType() == ValueType.NUMBER) {
     return ((JsonNumber) value).longValue();
   }
   return defaultValue;
 }
 /**
  * @param key
  * @return JsonObject value or null
  */
 public JsonObject getObject(String key) {
   JsonObject obj = getParsedBody();
   JsonValue value = obj.get(key);
   if (value != null && value.getValueType() == ValueType.OBJECT) {
     return (JsonObject) value;
   }
   return null;
 }
 /**
  * Get boolean value out of the message json payload
  *
  * @param key Key to find value for
  * @param defaultValue Default value if key can't be found
  * @return value in object, or the provided default
  */
 public boolean getBoolean(String key, boolean defaultValue) {
   JsonObject obj = getParsedBody();
   JsonValue value = obj.get(key);
   if (value != null) {
     if (value.getValueType() == ValueType.FALSE) return false;
     if (value.getValueType() == ValueType.TRUE) return true;
   }
   return defaultValue;
 }
  public void run() {
    Deque<VisitingContext> stack = new ArrayDeque<>();

    stack.add(new VisitingContext(rootNode));
    boolean goOn = stack.peek().hasNext();

    if (goOn) {
      do {
        Map.Entry<String, JsonValue> current = stack.peek().nextElement();

        if (!(current.getValue() instanceof JsonStructure)) {
          String key = stack.peek().getNSPrefix() + current.getKey();
          String value = null;
          JsonValue jsonValue = current.getValue();
          switch (jsonValue.getValueType()) {
            case NULL:
              value = null;
              break;
            case FALSE:
              value = Boolean.FALSE.toString();
              break;
            case TRUE:
              Boolean.TRUE.toString();
              break;
            case NUMBER:
              value = jsonValue.toString();
              break;
            case STRING:
              value = ((JsonString) jsonValue).getString();
              break;
            default:
              throw new ConfigException("Internal failure while processing JSON document.");
          }

          targetStore.put(key, value);
        } else if (current.getValue() instanceof JsonObject) {
          String key = stack.peek().getNSPrefix() + current.getKey();
          JsonObject node = (JsonObject) current.getValue();
          stack.push(new VisitingContext(node, key));
        } else if (current.getValue() instanceof JsonArray) {
          throw new ConfigException("Arrays are not supported at the moment.");
        } else {
          throw new ConfigException("Internal failure while processing JSON document.");
        }

        goOn = stack.peek().hasNext();

        while (!goOn && stack.size() > 0) {
          stack.remove();
          goOn = (stack.size() > 0) && stack.peek().hasNext();
        }
      } while (goOn);
    }
  }
 private void handleArray(
     final Writer writer,
     final String prefix,
     final Map<String, JsonObject> nestedTypes,
     final JsonValue value,
     final String jsonField,
     final String fieldName,
     final int arrayLevel,
     final Collection<String> imports)
     throws IOException {
   final JsonArray array = JsonArray.class.cast(value);
   if (array.size()
       > 0) { // keep it simple for now - 1 level, we can have an awesome recursive algo later if
              // needed
     final JsonValue jsonValue = array.get(0);
     switch (jsonValue.getValueType()) {
       case OBJECT:
         final String javaName = toJavaName(fieldName);
         nestedTypes.put(javaName, JsonObject.class.cast(jsonValue));
         fieldGetSetMethods(writer, jsonField, fieldName, javaName, prefix, arrayLevel, imports);
         break;
       case TRUE:
       case FALSE:
         fieldGetSetMethods(writer, jsonField, fieldName, "Boolean", prefix, arrayLevel, imports);
         break;
       case NUMBER:
         fieldGetSetMethods(writer, jsonField, fieldName, "Double", prefix, arrayLevel, imports);
         break;
       case STRING:
         fieldGetSetMethods(writer, jsonField, fieldName, "String", prefix, arrayLevel, imports);
         break;
       case ARRAY:
         handleArray(
             writer,
             prefix,
             nestedTypes,
             jsonValue,
             jsonField,
             fieldName,
             arrayLevel + 1,
             imports);
         break;
       case NULL:
       default:
         throw new UnsupportedOperationException("Unsupported " + value + ".");
     }
   } else {
     getLog().warn("Empty arrays are ignored");
   }
 }
  /**
   * http://docs.oracle.com/middleware/1213/wls/WLPRG/java-api-for-json-proc.htm#WLPRG1061
   *
   * @param tree
   * @param key
   */
  public static Object navigateTree(JsonValue tree, String key) {

    Object ret = null;

    if (key != null) System.out.print("Key " + key + ": ");

    switch (tree.getValueType()) {
      case OBJECT:
        System.out.println("OBJECT");
        JsonObject object = (JsonObject) tree;

        for (String name : object.keySet()) navigateTree(object.get(name), name);
        break;
      case ARRAY:
        System.out.println("ARRAY");
        JsonArray array = (JsonArray) tree;
        for (JsonValue val : array) navigateTree(val, null);
        break;
      case STRING:
        JsonString st = (JsonString) tree;
        System.out.println("STRING " + st.getString());
        break;
      case NUMBER:
        JsonNumber num = (JsonNumber) tree;
        System.out.println("NUMBER " + num.toString());
        break;
      case TRUE:
        System.out.println("TRUE");
      case FALSE:
        System.out.println("FALSE");
      case NULL:
        System.out.println(tree.getValueType().toString());
        break;
    }

    return ret;
  }
 /**
  * @param key
  * @return JsonArray value or null
  */
 public List<Long> getLongArray(String key, List<Long> defaultValue) {
   JsonObject obj = getParsedBody();
   JsonValue arrayValue = obj.get(key);
   if (arrayValue != null && arrayValue.getValueType() == ValueType.ARRAY) {
     try {
       List<Long> result = new ArrayList<>();
       ((JsonArray) arrayValue).forEach(value -> result.add(((JsonNumber) value).longValue()));
       return result;
     } catch (Exception e) { // class cast, etc
       Log.log(Level.FINER, this, "Exception parsing JsonArray: " + arrayValue, e);
       // fall through to return default value
     }
   }
   return defaultValue;
 }
  /**
   * Get optional long value out of the message json payload
   *
   * @param key Key to find value for
   * @return value in object or null
   */
  public String getString(String key) {
    String result = null;
    JsonObject obj = getParsedBody();
    JsonValue value = obj.get(key);
    if (value != null) {
      try {
        if (value.getValueType() == ValueType.STRING) {
          result = obj.getString(key);
        } else {
          result = value.toString();
        }
      } catch (Exception e) { // class cast, etc
        Log.log(Level.FINER, this, "Exception parsing String: " + value, e);
        // fall through to return default value
      }
    }

    return result;
  }
Beispiel #12
0
 private Object jsonValueToObject(JsonValue jsonValue) {
   switch (jsonValue.getValueType()) {
     case FALSE:
       return Boolean.FALSE;
     case TRUE:
       return Boolean.TRUE;
     case NULL:
       return null;
     case STRING:
       {
         JsonString casted = (JsonString) jsonValue;
         return casted.getString();
       }
     case NUMBER:
       {
         JsonNumber number = (JsonNumber) jsonValue;
         if (number.isIntegral()) {
           try {
             long l = number.longValueExact();
             if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
               return l;
             }
             return number.intValueExact();
           } catch (ArithmeticException ex) {
             return number.bigIntegerValueExact();
           }
         }
         return number.doubleValue();
       }
     case ARRAY:
       {
         assert jsonValue instanceof JsonArray;
         return (JsonArray) jsonValue;
       }
     case OBJECT:
     default:
       throw new AssertionError(jsonValue + " is not accepted as a column value");
   }
 }
Beispiel #13
0
 public static int navigateTree(JsonValue tree, String skip) {
   int ret = 0;
   switch (tree.getValueType()) {
     case OBJECT:
       // System.out.println("OBJECT");
       JsonArrayBuilder dirtyResult = Json.createArrayBuilder();
       JsonObject object = (JsonObject) tree;
       for (String name : object.keySet()) {
         if (object.get(name).getValueType() == JsonValue.ValueType.STRING
             && object.getString(name).equals(skip)) {
           return 0;
         }
         dirtyResult.add(object.get(name));
       }
       ret = ret + navigateTree(dirtyResult.build(), skip);
       break;
     case ARRAY:
       // System.out.println("ARRAY");
       JsonArray array = (JsonArray) tree;
       for (JsonValue val : array) ret = ret + navigateTree(val, skip);
       break;
     case STRING:
       JsonString st = (JsonString) tree;
       // System.out.println("STRING " + st.getString());
       break;
     case NUMBER:
       JsonNumber num = (JsonNumber) tree;
       return num.intValue();
     case TRUE:
     case FALSE:
     case NULL:
       // System.out.println(tree.getValueType().toString());
       break;
   }
   return ret;
 }