public static boolean isValidString(String s) {
    int length = s.length();
    int i = 0;
    while (true) {
      char ch;
      do {
        if (i == length) {
          return true;
        }
        ch = s.charAt(i);
        i++;
      } while (!Character.isSurrogate(ch));
      if (Character.isLowSurrogate(ch) || i == length || !Character.isLowSurrogate(s.charAt(i))) {
        return false;
      }
      i++;
    }
  }
A JSON Parser based on the GSON JsonReader.

<p>The parsing is almost identical to the normal parser provided by GSON with these changes: it
never uses "lenient" mode, it rejects duplicated map keys and it rejects strings with invalid
UTF16 characters.

<p>The implementation is adapted from almost identical to GSON's TypeAdapters.JSON_ELEMENT.
java_src/src/main/java/com/google/crypto/tink/internal/JsonParser.java