コード例 #1
0
 private static String a(JsonReader paramJsonReader) {
   if (paramJsonReader.peek() == JsonToken.NULL) {
     paramJsonReader.nextNull();
     return null;
   }
   return paramJsonReader.nextString();
 }
コード例 #2
0
  /**
   * import JSON as a new note.
   *
   * @param sourceJson - the note JSON to import
   * @param noteName - the name of the new note
   * @return notebook ID
   * @throws IOException
   */
  public Note importNote(String sourceJson, String noteName, AuthenticationInfo subject)
      throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();

    Gson gson =
        gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    JsonReader reader = new JsonReader(new StringReader(sourceJson));
    reader.setLenient(true);
    Note newNote;
    try {
      Note oldNote = gson.fromJson(reader, Note.class);
      newNote = createNote(subject);
      if (noteName != null) newNote.setName(noteName);
      else newNote.setName(oldNote.getName());
      List<Paragraph> paragraphs = oldNote.getParagraphs();
      for (Paragraph p : paragraphs) {
        newNote.addCloneParagraph(p);
      }

      newNote.persist(subject);
    } catch (IOException e) {
      logger.error(e.toString(), e);
      throw e;
    }

    return newNote;
  }
コード例 #3
0
 public Map<K, V> a(JsonReader paramJsonReader) throws IOException {
   Object localObject = paramJsonReader.peek();
   if (localObject == JsonToken.NULL) {
     paramJsonReader.nextNull();
     return null;
   }
   Map localMap = (Map) d.a();
   if (localObject == JsonToken.BEGIN_ARRAY) {
     paramJsonReader.beginArray();
     while (paramJsonReader.hasNext()) {
       paramJsonReader.beginArray();
       localObject = b.b(paramJsonReader);
       if (localMap.put(localObject, c.b(paramJsonReader)) != null) {
         throw new JsonSyntaxException("duplicate key: " + localObject);
       }
       paramJsonReader.endArray();
     }
     paramJsonReader.endArray();
     return localMap;
   }
   paramJsonReader.beginObject();
   while (paramJsonReader.hasNext()) {
     d.INSTANCE.promoteNameToValue(paramJsonReader);
     localObject = b.b(paramJsonReader);
     if (localMap.put(localObject, c.b(paramJsonReader)) != null) {
       throw new JsonSyntaxException("duplicate key: " + localObject);
     }
   }
   paramJsonReader.endObject();
   return localMap;
 }
コード例 #4
0
  @Override
  public Date read(JsonReader jsonReader) throws IOException {
    if (jsonReader.peek() == JsonToken.NULL) {
      jsonReader.nextNull();
      return null;
    }

    Date date = null;

    //        while(jsonReader.hasNext()) {
    if (jsonReader.peek().equals(JsonToken.BEGIN_OBJECT)) {
      jsonReader.beginObject();

      while (!jsonReader.peek().equals(JsonToken.END_OBJECT)) {
        JsonToken token = jsonReader.peek();
        switch (token) {
          case NAME:
            if (jsonReader.nextName().equals("$date")) {
              date = getDateFromString(jsonReader.nextString());
            }
            break;
          default:
            jsonReader.skipValue();
        }
      }
      jsonReader.endObject();
    }
    //      }

    return date;
  }
コード例 #5
0
    public BitSet read(JsonReader jsonreader)
    {
        JsonToken jsontoken;
        BitSet bitset;
        int i;
        if (jsonreader.peek() == JsonToken.NULL)
        {
            jsonreader.nextNull();
            return null;
        }
        bitset = new BitSet();
        jsonreader.beginArray();
        jsontoken = jsonreader.peek();
        i = 0;
_L2:
        boolean flag;
        if (jsontoken == JsonToken.END_ARRAY)
        {
            break MISSING_BLOCK_LABEL_209;
        }
        switch (..SwitchMap.com.google.gson.stream.JsonToken[jsontoken.ordinal()])
        {
        default:
            throw new JsonSyntaxException((new StringBuilder()).append("Invalid bitset value type: ").append(jsontoken).toString());

        case 2: // '\002'
            break; /* Loop/switch isn't completed */
コード例 #6
0
  private int importFile(File importPath, ImporterExporter importer) {
    File file = new File(importPath, importer.getFileName());
    if (!file.exists() || !file.canRead()) {
      return ERROR_FILE_ACCESS;
    }

    importer.initializeImport(mContext);

    try {
      InputStream in = new FileInputStream(file);

      Gson gson = new Gson();

      JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
      reader.beginArray();

      while (reader.hasNext()) {
        importer.importRecord(mContext, gson, reader);
      }

      reader.endArray();
      reader.close();
    } catch (JsonParseException | IOException e) {
      // the given Json might not be valid or unreadable
      Timber.e(e, "JSON show import failed");
      return ERROR;
    }

    return SUCCESS;
  }
コード例 #7
0
  private static boolean validateJSON(final File testOutputFile, final List<String> failures)
      throws FileNotFoundException, SAXException, IOException {
    final JsonElement jsonElement;
    try (final Reader reader = new BufferedReader(new FileReader(testOutputFile));
        final JsonReader jsonReader = new JsonReader(reader); ) {
      jsonElement = new JsonParser().parse(jsonReader);
      if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
        failures.add("JSON document was not fully consumed.");
      }
    } catch (final Exception e) {
      failures.add(e.getMessage());
      return false;
    }

    final int size;
    if (jsonElement.isJsonObject()) {
      size = jsonElement.getAsJsonObject().entrySet().size();
    } else if (jsonElement.isJsonArray()) {
      size = jsonElement.getAsJsonArray().size();
    } else {
      size = 0;
    }

    if (size == 0) {
      failures.add("Invalid JSON string");
    }

    return failures.isEmpty();
  }
コード例 #8
0
 public T read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   return nameToConstant.get(in.nextString());
 }
コード例 #9
0
ファイル: Gson.java プロジェクト: Jehyeok/SW_Architecting
 /**
  * Reads the next JSON value from {@code reader} and convert it to an object of type {@code
  * typeOfT}. Since Type is not parameterized by T, this method is type unsafe and should be used
  * carefully
  *
  * @throws JsonIOException if there was a problem writing to the Reader
  * @throws JsonSyntaxException if json is not a valid representation for an object of type
  */
 @SuppressWarnings("unchecked")
 public <T> T fromJson(JsonReader reader, Type typeOfT)
     throws JsonIOException, JsonSyntaxException {
   boolean isEmpty = true;
   boolean oldLenient = reader.isLenient();
   reader.setLenient(true);
   try {
     reader.peek();
     isEmpty = false;
     TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT);
     TypeAdapter<T> typeAdapter = getAdapter(typeToken);
     T object = typeAdapter.read(reader);
     return object;
   } catch (EOFException e) {
     /*
      * For compatibility with JSON 1.5 and earlier, we return null for empty
      * documents instead of throwing.
      */
     if (isEmpty) {
       return null;
     }
     throw new JsonSyntaxException(e);
   } catch (IllegalStateException e) {
     throw new JsonSyntaxException(e);
   } catch (IOException e) {
     // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException
     throw new JsonSyntaxException(e);
   } finally {
     reader.setLenient(oldLenient);
   }
 }
コード例 #10
0
ファイル: DateTypeAdapter.java プロジェクト: jqzhang/QingTing
 public Date read(JsonReader paramJsonReader) throws IOException {
   if (paramJsonReader.peek() == JsonToken.NULL) {
     paramJsonReader.nextNull();
     return null;
   }
   return deserializeToDate(paramJsonReader.nextString());
 }
コード例 #11
0
 @Override
 public StringFilter read(JsonReader reader) throws IOException {
   StringFilter result = null;
   // reset instance state
   this.type = null;
   this.delegate = null;
   reader.beginObject();
   while (reader.peek() != JsonToken.END_OBJECT) {
     String name = reader.nextName();
     try {
       if (this.delegate != null) {
         this.delegate.parseValue(reader, name);
       } else {
         parseValue(reader, name);
       }
     } catch (Exception ex) {
       LOG.error(ex.getMessage(), ex);
       throw new IOException(ex);
     }
   }
   reader.endObject();
   if (this.type != null) {
     try {
       if (this.delegate != null) {
         result = this.delegate.createInstance(this.type);
       } else {
         result = createInstance(this.type);
       }
     } catch (Exception ex) {
       LOG.error(ex.getMessage(), ex);
       throw new IOException(ex.toString());
     }
   }
   return result;
 }
コード例 #12
0
 @Override
 public Locale read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   String locale = in.nextString();
   StringTokenizer tokenizer = new StringTokenizer(locale, "_");
   String language = null;
   String country = null;
   String variant = null;
   if (tokenizer.hasMoreElements()) {
     language = tokenizer.nextToken();
   }
   if (tokenizer.hasMoreElements()) {
     country = tokenizer.nextToken();
   }
   if (tokenizer.hasMoreElements()) {
     variant = tokenizer.nextToken();
   }
   if (country == null && variant == null) {
     return new Locale(language);
   } else if (variant == null) {
     return new Locale(language, country);
   } else {
     return new Locale(language, country, variant);
   }
 }
コード例 #13
0
ファイル: Gson.java プロジェクト: gusfl/Tinder-repo
 public Number read(JsonReader jsonReader) throws IOException {
   if (jsonReader.peek() != JsonToken.NULL) {
     return Long.valueOf(jsonReader.nextLong());
   }
   jsonReader.nextNull();
   return null;
 }
コード例 #14
0
ファイル: Gson.java プロジェクト: gusfl/Tinder-repo
 public Float read(JsonReader jsonReader) throws IOException {
   if (jsonReader.peek() != JsonToken.NULL) {
     return Float.valueOf((float) jsonReader.nextDouble());
   }
   jsonReader.nextNull();
   return null;
 }
コード例 #15
0
 private static void readJSON() {
   Gson gson = new GsonBuilder().create();
   JsonReader reader = null;
   try {
     reader = new JsonReader(new FileReader("html_files.json"));
     htmlJSON = gson.fromJson(reader, HashMap.class);
     reader.close();
     reader = new JsonReader(new FileReader("termID.json"));
     termsIDSMap = gson.fromJson(reader, TreeMap.class);
     reader.close();
     reader = new JsonReader(new FileReader("index.json"));
     reverseIndex = gson.fromJson(reader, HashMap.class);
   } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } finally {
     if (reader != null) {
       try {
         reader.close();
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
コード例 #16
0
  @Override
  public void onMessage(byte[] data) {

    try {

      String message = new String(data, "UTF8");

      JsonReader reader = new JsonReader(new StringReader(message));
      reader.setLenient(true);

      // pretty print use this
      Gson gson = new GsonBuilder().setPrettyPrinting().create();

      // normal print, use this
      // Gson gson = new Gson();

      HeartbeatMessage hbm = gson.fromJson(reader, HeartbeatMessage.class);

      String jsonOutput = gson.toJson(hbm);

      logger.info("received heart beat: " + jsonOutput);

    } catch (Exception e) {
      logger.log(Level.WARNING, e.getMessage(), e);
    }
  }
コード例 #17
0
 @Override
 public UUID read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   return java.util.UUID.fromString(in.nextString());
 }
コード例 #18
0
 @Override
 public StringBuffer read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   return new StringBuffer(in.nextString());
 }
コード例 #19
0
 @Override
 public Number read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   return in.nextDouble();
 }
コード例 #20
0
 @Override
 public Boolean read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   return Boolean.valueOf(in.nextString());
 }
コード例 #21
0
ファイル: DateTypeAdapter.java プロジェクト: CAG12/ambrosia2
 @Override
 public Date read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   return deserializeToDate(in.nextString());
 }
コード例 #22
0
ファイル: r.java プロジェクト: a54110/MiBandDecompiled
 public Object read(JsonReader jsonreader) {
   if (jsonreader.peek() == JsonToken.NULL) {
     jsonreader.nextNull();
     return null;
   } else {
     return a.read(jsonreader);
   }
 }
コード例 #23
0
 @Override
 public void consume(JsonReader in, JsonSchemaObject object) throws IOException {
   if (in.peek() == JsonToken.NUMBER) {
     readNumber(in, object);
   } else {
     in.skipValue();
   }
 }
コード例 #24
0
 @Override
 public void consume(JsonReader in, JsonSchemaObject object) throws IOException {
   if (in.peek() == JsonToken.BOOLEAN) {
     assign(in.nextBoolean(), object);
   } else {
     in.skipValue();
   }
 }
コード例 #25
0
 @Override
 public URL read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   String nextString = in.nextString();
   return "null".equals(nextString) ? null : new URL(nextString);
 }
コード例 #26
0
ファイル: TypeAdapter.java プロジェクト: SkyLB2016/HbAndGap
 /**
  * Converts {@code jsonTree} to a Java object.
  *
  * @param jsonTree the Java object to convert. May be {@link JsonNull}.
  */
 /*public*/ final T fromJsonTree(JsonElement jsonTree) {
   try {
     JsonReader jsonReader = new JsonTreeReader(jsonTree);
     jsonReader.setLenient(true);
     return read(jsonReader);
   } catch (IOException e) {
     throw new JsonIOException(e);
   }
 }
コード例 #27
0
 @Override
 public InetAddress read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   }
   // regrettably, this should have included both the host name and the host address
   return InetAddress.getByName(in.nextString());
 }
コード例 #28
0
ファイル: QuestAdapter.java プロジェクト: Bunsan/HQM
 private ItemStack[] readItemStackArray(JsonReader in) throws IOException {
   List<ItemStack> stacks = new ArrayList<>();
   in.beginArray();
   while (in.hasNext()) {
     ItemStack stack = MinecraftAdapter.ITEM_STACK.read(in);
     if (stack != null) stacks.add(stack);
   }
   in.endArray();
   return stacks.toArray(new ItemStack[stacks.size()]);
 }
コード例 #29
0
 protected void loadModel(InputStream model_stream) throws Exception {
   Gson gson = new Gson();
   labelMap = new HashMap<String, Integer>();
   JsonReader br = new JsonReader(new InputStreamReader(model_stream));
   br.setLenient(true);
   model = gson.fromJson(br, Model.class);
   for (int i = 0; i < model.labels.size(); i++) {
     labelMap.put(model.labels.get(i), i);
   }
 }
コード例 #30
0
 @Override
 public Class read(JsonReader in) throws IOException {
   if (in.peek() == JsonToken.NULL) {
     in.nextNull();
     return null;
   } else {
     throw new UnsupportedOperationException(
         "Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?");
   }
 }