Java Code Examples for com.google.gson.stream.JsonReader#endArray()

The following examples show how to use com.google.gson.stream.JsonReader#endArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeHandler.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public void read(JsonReader in, GsonProperty property, Object t) throws IOException {
    final Class<?> simpleType = property.getType();

    List list = new ArrayList();
    in.beginArray();
    if (simpleType.isPrimitive() || isBoxedClass(simpleType)) {
        while (in.hasNext()){
            list.add(SupportUtils.readPrimitiveOrItsBox(in, property));
        }
    }else{
        TypeAdapter adapter = getTypeAdapter(simpleType);
        while (in.hasNext()){
            list.add(adapter.read(in));
        }
    }
    //copy to array
    Object array = Array.newInstance(simpleType, list.size());
    for(int i = 0, size = list.size() ; i < size ; i ++){
        Array.set(array, i, list.get(i));
    }
    setValue(property, t, array);
    in.endArray();
}
 
Example 2
Source File: DesignateAdapters.java    From denominator with Apache License 2.0 6 votes vote down vote up
@Override
public List<X> read(JsonReader reader) throws IOException {
  List<X> elements = new LinkedList<X>();
  reader.beginObject();
  while (reader.hasNext()) {
    String nextName = reader.nextName();
    if (jsonKey().equals(nextName)) {
      reader.beginArray();
      while (reader.hasNext()) {
        reader.beginObject();
        elements.add(build(reader));
        reader.endObject();
      }
      reader.endArray();
    } else {
      reader.skipValue();
    }
  }
  reader.endObject();
  Collections.sort(elements, toStringComparator());
  return elements;
}
 
Example 3
Source File: ArrayTypeAdapter.java    From gson with Apache License 2.0 6 votes vote down vote up
@Override public Object read(JsonReader in) throws IOException {
  if (in.peek() == JsonToken.NULL) {
    in.nextNull();
    return null;
  }

  List<E> list = new ArrayList<E>();
  in.beginArray();
  while (in.hasNext()) {
    E instance = componentTypeAdapter.read(in);
    list.add(instance);
  }
  in.endArray();

  int size = list.size();
  Object array = Array.newInstance(componentType, size);
  for (int i = 0; i < size; i++) {
    Array.set(array, i, list.get(i));
  }
  return array;
}
 
Example 4
Source File: StreamAclJsonAdapter.java    From esjc with MIT License 6 votes vote down vote up
private static List<String> readRoles(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.STRING) {
        return singletonList(reader.nextString());
    } else {
        List<String> roles = new ArrayList<>();

        reader.beginArray();

        while (reader.peek() != JsonToken.END_ARRAY) {
            roles.add(reader.nextString());
        }

        reader.endArray();

        return roles;
    }
}
 
Example 5
Source File: SBSApiBase.java    From iview-android-tv with MIT License 6 votes vote down vote up
private List<EpisodeModel> readEntriesArray(JsonReader reader) throws IOException {
    reader.beginArray();
    Gson gson = new GsonBuilder().create();
    List<EpisodeModel> all = new ArrayList<>();
    while (reader.hasNext()) {
        try {
            Entry entry = gson.fromJson(reader, Entry.class);
            EpisodeModel ep = EpisodeModel.create(entry);
            all.add(ep);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
    }
    reader.endArray();
    return all;
}
 
Example 6
Source File: SizeWeightReader.java    From TFC2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process(JsonReader reader)
{
	try 
	{
		reader.beginArray();

		while(reader.hasNext())
		{
			list.add(read(reader));
		}
		reader.endArray();
		reader.close();

	} catch (IOException e) 
	{
		e.printStackTrace();
	}
}
 
Example 7
Source File: UsersStreamDeserializer.java    From java-json-benchmark with MIT License 6 votes vote down vote up
@Override
public Users gson(JsonReader reader) throws IOException {
    Users uc = new Users();
    reader.beginObject();

    JsonToken token;
    while ((token = reader.peek()) != JsonToken.END_OBJECT) {
        if (token == JsonToken.NAME) {
            String fieldname = reader.nextName();
            if ("users".equals(fieldname)) {
                uc.users = new ArrayList<>();
                reader.beginArray();
                while (reader.peek() != JsonToken.END_ARRAY) {
                    uc.users.add(gsonUser(reader));
                }
                reader.endArray();
            }
        }
    }
    reader.endObject();
    return uc;
}
 
Example 8
Source File: ObjectTypeAdapter.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public Object read(JsonReader in) throws IOException {
  JsonToken token = in.peek();
  switch (token) {
  case BEGIN_ARRAY:
    List<Object> list = new ArrayList<Object>();
    in.beginArray();
    while (in.hasNext()) {
      list.add(read(in));
    }
    in.endArray();
    return list;

  case BEGIN_OBJECT:
    Map<String, Object> map = new LinkedTreeMap<String, Object>();
    in.beginObject();
    while (in.hasNext()) {
      map.put(in.nextName(), read(in));
    }
    in.endObject();
    return map;

  case STRING:
    return in.nextString();

  case NUMBER:
    return in.nextDouble();

  case BOOLEAN:
    return in.nextBoolean();

  case NULL:
    in.nextNull();
    return null;

  default:
    throw new IllegalStateException();
  }
}
 
Example 9
Source File: ObjectTypeAdapter2.java    From SimpleProject with MIT License 5 votes vote down vote up
@Override
public List<T> read(JsonReader in) throws IOException {
	JsonToken token = in.peek();

	switch (token) {
		case BEGIN_ARRAY:
			List<T> list = new ArrayList<>();
			in.beginArray();
			in.endArray();
			return list.size() > 0 ? list : null;
		default:
			throw new IllegalStateException();
	}
}
 
Example 10
Source File: BufferAttribute.java    From BlueMap with MIT License 5 votes vote down vote up
public static BufferAttribute readJson(JsonReader json) throws IOException {
	List<Float> list = new ArrayList<>(1000);
	int itemSize = 1;
	boolean normalized = false;
	
	json.beginObject(); //root
	while (json.hasNext()){
		String name = json.nextName();
		
		if(name.equals("array")){
			json.beginArray(); //array
			while (json.hasNext()){
				list.add(new Float(json.nextDouble()));
			}
			json.endArray(); //array
		}
		
		else if (name.equals("itemSize")) {
			itemSize = json.nextInt();
		}
		
		else if (name.equals("normalized")) {
			normalized = json.nextBoolean();
		}
		
		else json.skipValue();
	}
	json.endObject(); //root
	
	//collect values in array
	float[] values = new float[list.size()];
	for (int i = 0; i < values.length; i++) {
		values[i] = list.get(i);
	}
	
	return new BufferAttribute(values, itemSize, normalized);
}
 
Example 11
Source File: TupleTypeAdapters.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Two<F, S> read(final JsonReader in) throws IOException {
	JsonToken next = in.peek();
	if (next == JsonToken.NULL) {
		in.nextNull();
		return null;
	}
	in.beginArray();
	F f = first.read(in);
	S s = second.read(in);
	Two<F, S> result = new Two<F, S>(f, s);
	in.endArray();
	return result;
}
 
Example 12
Source File: JsonUtil.java    From yunpian-java-sdk with MIT License 5 votes vote down vote up
private static Object read2Str(JsonReader in, boolean str) throws IOException {
    JsonToken token = in.peek();
    switch (token) {
    case BEGIN_ARRAY:
        List<Object> list = new LinkedList<Object>();
        in.beginArray();
        while (in.hasNext()) {
            list.add(read2Str(in, false));
        }
        in.endArray();
        return str ? JsonUtil.toJson(list) : list;

    case BEGIN_OBJECT:
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        in.beginObject();
        while (in.hasNext()) {
            map.put(in.nextName(), read2Str(in, false));
        }
        in.endObject();
        return str ? JsonUtil.toJson(map) : map;

    case STRING:
        return in.nextString();

    case NUMBER:
        return in.nextString();
    case BOOLEAN:
        return in.nextBoolean();

    case NULL:
        in.nextNull();
        return "";
    default:
        return in.nextString();
    }
}
 
Example 13
Source File: TransactionsTypeAdapter.java    From greenbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Transactions read(JsonReader reader) throws IOException {
	reader.beginObject();
	checkState(reader.nextName().equals(NAME_TRANSACTIONS));
	reader.beginArray();

	ImmutableList.Builder<Transaction> elements = ImmutableList.builder();
	while (reader.hasNext()) {
		elements.add(readTransaction(reader.nextString()));
	}

	reader.endArray();
	reader.endObject();
	return new Transactions(elements.build());
}
 
Example 14
Source File: BoundProperty.java    From ForgeHax with MIT License 5 votes vote down vote up
@Override
public void deserialize(JsonReader reader) throws IOException {
  reader.beginArray();
  while (reader.hasNext()) {
    reader.beginArray();
    add(reader.nextInt(), reader.nextInt());
    reader.endArray();
  }
  reader.endArray();
}
 
Example 15
Source File: JsonParser.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static List<?> parseJsonArray(JsonReader jr) throws IOException {
  jr.beginArray();
  List<Object> array = new ArrayList<>();
  while (jr.hasNext()) {
    Object value = parseRecursive(jr);
    array.add(value);
  }
  checkState(jr.peek() == JsonToken.END_ARRAY, "Bad token: " + jr.getPath());
  jr.endArray();
  return Collections.unmodifiableList(array);
}
 
Example 16
Source File: MixedStreamTest.java    From gson with Apache License 2.0 5 votes vote down vote up
public void testReadMixedStreamed() throws IOException {
  Gson gson = new Gson();
  StringReader stringReader = new StringReader(CARS_JSON);
  JsonReader jsonReader = new JsonReader(stringReader);

  jsonReader.beginArray();
  assertEquals(BLUE_MUSTANG, gson.fromJson(jsonReader, Car.class));
  assertEquals(BLACK_BMW, gson.fromJson(jsonReader, Car.class));
  assertEquals(RED_MIATA, gson.fromJson(jsonReader, Car.class));
  jsonReader.endArray();
}
 
Example 17
Source File: ExtractorsTypeAdapter.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
@Override
public Extractors read(JsonReader in) throws IOException {
    Extractors extractors = new Extractors();

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
            case Extractors.TYPE_FUNC:
                in.beginArray();
                List<String> funcs = new ArrayList<>();
                while (in.hasNext()) {
                    String aFunc = in.nextString();
                    aFunc = fixPattern(aFunc);
                    funcs.add(aFunc);
                }
                extractors.setFunc(funcs);
                in.endArray();
                break;
            case Extractors.TYPE_FILENAME:
                in.beginArray();
                List<String> filenames = new ArrayList<>();
                while (in.hasNext()) {
                    String aFilename = in.nextString();
                    aFilename = fixPattern(aFilename);
                    filenames.add(aFilename);
                }
                extractors.setFilename(filenames);
                in.endArray();
                break;
            case Extractors.TYPE_FILECONTENT:
                in.beginArray();
                List<String> filecontents = new ArrayList<>();
                while (in.hasNext()) {
                    String aFilecontent = in.nextString();
                    aFilecontent = fixPattern(aFilecontent);
                    filecontents.add(aFilecontent);
                }
                extractors.setFilecontent(filecontents);
                in.endArray();
                break;
            case Extractors.TYPE_URI:
                in.beginArray();
                List<String> uris = new ArrayList<>();
                while (in.hasNext()) {
                    String aUri = in.nextString();
                    aUri = fixPattern(aUri);
                    uris.add(aUri);
                }
                extractors.setUri(uris);
                in.endArray();
                break;
            case Extractors.TYPE_HASHES:
                in.beginObject();
                Map<String, String> hashes = new HashMap<String, String>();
                while (in.hasNext()) {
                    String key = in.nextName();
                    String value = in.nextString();
                    hashes.put(key, value);
                }
                extractors.setHashes(hashes);
                in.endObject();
                break;
            case "filecontentreplace":
                in.beginArray();
                while (in.hasNext()) {
                    @SuppressWarnings("unused")
                    String ignore = in.nextString(); // Ignore
                }
                in.endArray();
                break;
        }
    }
    in.endObject();

    return extractors;
}
 
Example 18
Source File: ProteusTypeAdapterFactory.java    From proteus with Apache License 2.0 4 votes vote down vote up
@Override
public Value read(JsonReader in) throws IOException {
  switch (in.peek()) {
    case STRING:
      return compileString(getContext(), in.nextString());
    case NUMBER:
      String number = in.nextString();
      return new Primitive(new LazilyParsedNumber(number));
    case BOOLEAN:
      return new Primitive(in.nextBoolean());
    case NULL:
      in.nextNull();
      return Null.INSTANCE;
    case BEGIN_ARRAY:
      Array array = new Array();
      in.beginArray();
      while (in.hasNext()) {
        array.add(read(in));
      }
      in.endArray();
      return array;
    case BEGIN_OBJECT:
      ObjectValue object = new ObjectValue();
      in.beginObject();
      if (in.hasNext()) {
        String name = in.nextName();
        if (ProteusConstants.TYPE.equals(name) && JsonToken.STRING.equals(in.peek())) {
          String type = in.nextString();
          if (PROTEUS_INSTANCE_HOLDER.isLayout(type)) {
            Layout layout = LAYOUT_TYPE_ADAPTER.read(type, PROTEUS_INSTANCE_HOLDER.getProteus(), in);
            in.endObject();
            return layout;
          } else {
            object.add(name, compileString(getContext(), type));
          }
        } else {
          object.add(name, read(in));
        }
      }
      while (in.hasNext()) {
        object.add(in.nextName(), read(in));
      }
      in.endObject();
      return object;
    case END_DOCUMENT:
    case NAME:
    case END_OBJECT:
    case END_ARRAY:
    default:
      throw new IllegalArgumentException();
  }
}
 
Example 19
Source File: MessageJsonSerializer.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public Message read(JsonReader in) throws IOException {
    in.beginObject();
    Message.Kind kind = Message.Kind.UNKNOWN;
    String text = "";
    String rawMessage = null;
    Optional<String> toolName = Optional.absent();
    ImmutableList.Builder<SourceFilePosition> positions =
            new ImmutableList.Builder<SourceFilePosition>();
    SourceFile legacyFile = SourceFile.UNKNOWN;
    SourcePosition legacyPosition = SourcePosition.UNKNOWN;
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.equals(KIND)) {
            //noinspection StringToUpperCaseOrToLowerCaseWithoutLocale
            Message.Kind theKind = KIND_STRING_ENUM_MAP.inverse()
                    .get(in.nextString().toLowerCase());
            kind = (theKind != null) ? theKind : Message.Kind.UNKNOWN;
        } else if (name.equals(TEXT)) {
            text = in.nextString();
        } else if (name.equals(RAW_MESSAGE)) {
            rawMessage = in.nextString();
        } else if (name.equals(TOOL_NAME)) {
            toolName = Optional.of(in.nextString());
        } else if (name.equals(SOURCE_FILE_POSITIONS)) {
            switch (in.peek()) {
                case BEGIN_ARRAY:
                    in.beginArray();
                    while(in.hasNext()) {
                        positions.add(mSourceFilePositionTypeAdapter.read(in));
                    }
                    in.endArray();
                    break;
                case BEGIN_OBJECT:
                    positions.add(mSourceFilePositionTypeAdapter.read(in));
                    break;
                default:
                    in.skipValue();
                    break;
            }
        } else if (name.equals(LEGACY_SOURCE_PATH)) {
            legacyFile = new SourceFile(new File(in.nextString()));
        } else if (name.equals(LEGACY_POSITION)) {
            legacyPosition = mSourcePositionTypeAdapter.read(in);
        } else {
            in.skipValue();
        }
    }
    in.endObject();

    if (legacyFile != SourceFile.UNKNOWN || legacyPosition != SourcePosition.UNKNOWN) {
        positions.add(new SourceFilePosition(legacyFile, legacyPosition));
    }
    if (rawMessage == null) {
        rawMessage = text;
    }
    ImmutableList<SourceFilePosition> sourceFilePositions = positions.build();
    if (!sourceFilePositions.isEmpty()) {
        return new Message(kind, text, rawMessage, toolName, sourceFilePositions);
    } else {
        return new Message(kind, text, rawMessage, toolName, ImmutableList.of(SourceFilePosition.UNKNOWN));
    }
}
 
Example 20
Source File: ImportInputFormat.java    From emr-dynamodb-connector with Apache License 2.0 4 votes vote down vote up
/**
 * This method retrieves the URLs of all S3 files and generates input splits by combining
 * multiple S3 URLs into one split.
 *
 * @return a list of input splits. The length of this list may not be exactly the same as
 * <code>numSplits</code>. For example, if numSplits is larger than MAX_NUM_SPLITS or the number
 * of S3 files, then numSplits is ignored. Furthermore, not all input splits contain the same
 * number of S3 files. For example, with five S3 files {s1, s2, s3, s4, s5} and numSplits = 3,
 * this method returns a list of three input splits: {s1, s2}, {s3, s4} and {s5}.
 */
private List<InputSplit> readEntries(JsonReader reader, JobConf job) throws IOException {
  List<Path> paths = new ArrayList<Path>();
  Gson gson = DynamoDBUtil.getGson();

  reader.beginArray();
  while (reader.hasNext()) {
    ExportManifestEntry entry = gson.fromJson(reader, ExportManifestEntry.class);
    paths.add(new Path(entry.url));
  }
  reader.endArray();
  log.info("Number of S3 files: " + paths.size());

  if (paths.size() == 0) {
    return Collections.emptyList();
  }

  int filesPerSplit = (int) Math.ceil((double) (paths.size()) / Math.min(MAX_NUM_SPLITS, paths
      .size()));
  int numSplits = (int) Math.ceil((double) (paths.size()) / filesPerSplit);

  long[] fileMaxLengths = new long[filesPerSplit];
  Arrays.fill(fileMaxLengths, Long.MAX_VALUE / filesPerSplit);

  long[] fileStarts = new long[filesPerSplit];
  Arrays.fill(fileStarts, 0);

  List<InputSplit> splits = new ArrayList<InputSplit>(numSplits);
  for (int i = 0; i < numSplits; i++) {
    int start = filesPerSplit * i;
    int end = filesPerSplit * (i + 1);
    if (i == (numSplits - 1)) {
      end = paths.size();
    }
    Path[] pathsInOneSplit = paths.subList(start, end).toArray(new Path[end - start]);
    CombineFileSplit combineFileSplit = new CombineFileSplit(job, pathsInOneSplit, fileStarts,
        fileMaxLengths, new String[0]);
    splits.add(combineFileSplit);
  }

  return splits;
}