Beispiel #1
0
 public Number read(JsonReader jsonReader) throws IOException {
   if (jsonReader.peek() != JsonToken.NULL) {
     return Long.valueOf(jsonReader.nextLong());
   }
   jsonReader.nextNull();
   return null;
 }
Beispiel #2
0
 @Override
 public Number read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   try {
     return in.nextLong();
   } catch (NumberFormatException e) {
     throw new JsonSyntaxException(e);
   }
 }
 private static Number a(JsonReader paramJsonReader) {
   if (paramJsonReader.peek() == JsonToken.NULL) {
     paramJsonReader.nextNull();
     return null;
   }
   try {
     long l = paramJsonReader.nextLong();
     return Long.valueOf(l);
   } catch (NumberFormatException paramJsonReader) {
     throw new JsonSyntaxException(paramJsonReader);
   }
 }
 private String nextValue() {
   try {
     tokenType = jsonReader.peek();
     JsonObject peek = stackObj.peek();
     String valueType = peek.getValueType();
     if (!validateArgumentTypes(tokenType, valueType)) {
       log.error(
           "Value type miss match, Expected value type - '"
               + valueType
               + "', but found - '"
               + tokenType.toString()
               + "'");
       throw new IllegalArgumentException(
           "Value type miss match, Expected value type - '"
               + valueType
               + "', but found - '"
               + tokenType.toString()
               + "'");
     }
     if (tokenType == JsonToken.STRING) {
       value = jsonReader.nextString();
     } else if (tokenType == JsonToken.BOOLEAN) {
       value = String.valueOf(jsonReader.nextBoolean());
     } else if (tokenType == JsonToken.NUMBER) {
       if (valueType.equals("int")) {
         value = String.valueOf(jsonReader.nextInt());
       } else if (valueType.equals("long")) {
         value = String.valueOf(jsonReader.nextLong());
       } else if (valueType.equals("double")) {
         value = String.valueOf(jsonReader.nextDouble());
       } else if (valueType.equals("float")) {
         value = String.valueOf(jsonReader.nextDouble());
       }
     } else if (tokenType == JsonToken.NULL) {
       jsonReader.nextNull();
       value = null;
     } else {
       log.error("Couldn't read the value, Illegal state exception");
       throw new RuntimeException("Couldn't read the value, Illegal state exception");
     }
   } catch (IOException e) {
     log.error("IO error while reading json stream");
     throw new RuntimeException("IO error while reading json stream");
   }
   return value;
 }
  private void jsonReadContElements(JsonReader jReader) throws IOException {
    jReader.beginObject();
    List<String> loadedLfs = new ArrayList<>();
    boolean exceptForDecimal5Raised = false;
    boolean enumChecked = false;
    boolean bitsChecked = false;
    boolean lfdecimal6Checked = false;
    boolean lfdecimal4Checked = false;
    boolean lfdecimal3Checked = false;
    boolean lfdecimal2Checked = false;
    boolean lfdecimal1Checked = false;
    boolean lfbool1Checked = false;
    boolean lfbool2Checked = false;
    boolean lfstrChecked = false;
    boolean lfbinaryChecked = false;
    // boolean lfref1Checked = false;
    boolean lfemptyChecked = false;
    boolean lfstr1Checked = false;

    while (jReader.hasNext()) {
      String keyName = jReader.nextName();
      JsonToken peek = null;
      try {
        peek = jReader.peek();
      } catch (IOException e) {
        if (keyName.equals("lfdecimal5")) {
          exceptForDecimal5Raised = true;
        } else {
          assertTrue("Key " + keyName + " has incorrect value for specifed type", false);
        }
      }

      if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        try {
          jReader.nextLong();
        } catch (NumberFormatException e) {
          assertTrue("Key " + keyName + " has incorrect value - " + e.getMessage(), false);
        }
        loadedLfs.add(keyName.substring(3));
      } else if (keyName.equals("lfstr")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
        assertEquals("lfstr", jReader.nextString());
        lfstrChecked = true;
      } else if (keyName.equals("lfstr1")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
        assertEquals("", jReader.nextString());
        lfstr1Checked = true;
      } else if (keyName.equals("lfbool1")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
        assertEquals(true, jReader.nextBoolean());
        lfbool1Checked = true;
      } else if (keyName.equals("lfbool2")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
        assertEquals(false, jReader.nextBoolean());
        lfbool2Checked = true;
      } else if (keyName.equals("lfbool3")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
        assertEquals(false, jReader.nextBoolean());
      } else if (keyName.equals("lfdecimal1")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(43.32), (Double) jReader.nextDouble());
        lfdecimal1Checked = true;
      } else if (keyName.equals("lfdecimal2")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(-0.43), (Double) jReader.nextDouble());
        lfdecimal2Checked = true;
      } else if (keyName.equals("lfdecimal3")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(43), (Double) jReader.nextDouble());
        lfdecimal3Checked = true;
      } else if (keyName.equals("lfdecimal4")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(43E3), (Double) jReader.nextDouble());
        lfdecimal4Checked = true;
      } else if (keyName.equals("lfdecimal6")) {
        assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
        assertEquals(new Double(33.12345), (Double) jReader.nextDouble());
        lfdecimal6Checked = true;
      } else if (keyName.equals("lfenum")) {
        assertEquals("enum3", jReader.nextString());
        enumChecked = true;
      } else if (keyName.equals("lfbits")) {
        assertEquals("bit3", jReader.nextString());
        bitsChecked = true;
      } else if (keyName.equals("lfbinary")) {
        assertEquals(
            "AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%%-#^",
            jReader.nextString());
        lfbinaryChecked = true;
      } else if (keyName.equals("lfempty")) {
        jReader.beginArray();
        jReader.nextNull();
        jReader.endArray();
        lfemptyChecked = true;
      } else if (keyName.startsWith("lfunion")) {
        checkLfUnion(jReader, keyName, peek);
      } else {
        assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
      }
    }
    Collections.sort(loadedLfs);
    String expectedLfsStr =
        "[int16Max, int16Min, int32Max, int32Min, int64Max, int64Min, int8Max, int8Min, uint16Max, uint32Max, uint8Max]";
    String actualLfsStr = loadedLfs.toString();
    assertEquals("Some leaves are missing", expectedLfsStr, actualLfsStr);
    // assertTrue("For lfdecimal5 wasn't catch error",exceptForDecimal5Raised);
    assertTrue("Enum wasn't checked", enumChecked);
    assertTrue("Bits wasn't checked", bitsChecked);
    assertTrue("Decimal1 wasn't checked", lfdecimal1Checked);
    assertTrue("Decimal2 wasn't checked", lfdecimal2Checked);
    assertTrue("Decimal3 wasn't checked", lfdecimal3Checked);
    assertTrue("Decimal4 wasn't checked", lfdecimal4Checked);
    assertTrue("Decimal5 wasn't checked", lfdecimal6Checked);
    assertTrue("lfbool1 wasn't checked", lfbool1Checked);
    assertTrue("lfbool2 wasn't checked", lfbool2Checked);
    assertTrue("lfstr wasn't checked", lfstrChecked);
    assertTrue("lfstr1 wasn't checked", lfstr1Checked);
    assertTrue("lfbinary wasn't checked", lfbinaryChecked);
    assertTrue("lfempty wasn't checked", lfemptyChecked);
    // assertTrue("lfref1 wasn't checked", lfref1Checked);

    jReader.endObject();
  }