Example usage for com.google.gson.stream JsonReader nextInt

List of usage examples for com.google.gson.stream JsonReader nextInt

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextInt.

Prototype

public int nextInt() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#NUMBER int value of the next token, consuming it.

Usage

From source file:us.blanshard.sudoku.game.GameJson.java

License:Apache License

/**
 * Registers type adapters in the given builder so that history lists and undo
 * stacks can be serialized and deserialized.  Note that undo stacks require a
 * CommandFactory be established before deserialization; see {@link #setFactory}.
 *//*from   w w  w .  j  a va2  s. c o m*/
public static GsonBuilder register(GsonBuilder builder) {

    builder.registerTypeHierarchyAdapter(Move.class, new TypeAdapter<Move>() {
        @Override
        public void write(JsonWriter out, Move value) throws IOException {
            out.value(value.toJsonValue());
        }

        @Override
        public Move read(JsonReader in) throws IOException {
            return Move.fromJsonValue(in.nextString());
        }
    });

    final TypeAdapter<Command> commandAdapter = new TypeAdapter<Command>() {
        @Override
        public void write(JsonWriter out, Command value) throws IOException {
            out.value(value.toJsonValue());
        }

        @Override
        public Command read(JsonReader in) throws IOException {
            Iterator<String> values = SPLITTER.split(in.nextString()).iterator();
            String type = values.next();
            return factorySlot.get().toCommand(type, values);
        }
    };
    builder.registerTypeHierarchyAdapter(Command.class, commandAdapter);

    builder.registerTypeAdapter(UndoStack.class, new TypeAdapter<UndoStack>() {
        @Override
        public void write(JsonWriter out, UndoStack value) throws IOException {
            out.beginObject();
            out.name("position").value(value.getPosition());
            out.name("commands").beginArray();
            for (Command c : value.commands)
                commandAdapter.write(out, c);
            out.endArray();
            out.endObject();
        }

        @Override
        public UndoStack read(JsonReader in) throws IOException {
            int position = -1;
            List<Command> commands = null;
            in.beginObject();
            while (in.hasNext()) {
                String name = in.nextName();
                if (name.equals("position")) {
                    position = in.nextInt();
                } else if (name.equals("commands")) {
                    commands = Lists.newArrayList();
                    in.beginArray();
                    while (in.hasNext())
                        commands.add(commandAdapter.read(in));
                    in.endArray();
                } else {
                    in.skipValue();
                }
            }
            in.endObject();
            return new UndoStack(commands, position);
        }
    });

    return builder;
}