private void writeImage(JsonGenerator generator, Image image) throws IOException {
   generator.writeStartObject();
   generator.writeFieldName(FIELD_URI);
   generator.writeString(image.uri);
   if (image.title != null) {
     generator.writeFieldName(FIELD_TITLE);
     generator.writeString(image.title);
   }
   generator.writeFieldName(FIELD_WIDTH);
   generator.writeNumber(image.width);
   generator.writeFieldName(FIELD_HEIGHT);
   generator.writeNumber(image.height);
   generator.writeFieldName(FIELD_SIZE);
   generator.writeString(image.size.name());
   generator.writeEndObject();
 }
 public void testSimpleArrayWrite() throws Exception {
   StringWriter sw = new StringWriter();
   JsonGenerator gen = new JsonFactory().createGenerator(sw);
   gen.writeStartArray();
   gen.writeNumber(13);
   gen.writeBoolean(true);
   gen.writeString("foobar");
   gen.writeEndArray();
   gen.close();
   String docStr = sw.toString();
   JsonParser jp = createParserUsingReader(docStr);
   assertEquals(JsonToken.START_ARRAY, jp.nextToken());
   assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
   assertEquals(13, jp.getIntValue());
   assertEquals(JsonToken.VALUE_TRUE, jp.nextToken());
   assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
   assertEquals("foobar", jp.getText());
   assertEquals(JsonToken.END_ARRAY, jp.nextToken());
   assertEquals(null, jp.nextToken());
   jp.close();
 }
 private void writeMedia(JsonGenerator generator, Media media) throws IOException {
   generator.writeStartObject();
   generator.writeFieldName(FIELD_PLAYER);
   generator.writeString(media.player.name());
   generator.writeFieldName(FIELD_URI);
   generator.writeString(media.uri);
   if (media.title != null) {
     generator.writeFieldName(FIELD_TITLE);
     generator.writeString(media.title);
   }
   generator.writeFieldName(FIELD_WIDTH);
   generator.writeNumber(media.width);
   generator.writeFieldName(FIELD_HEIGHT);
   generator.writeNumber(media.height);
   generator.writeFieldName(FIELD_FORMAT);
   generator.writeString(media.format);
   generator.writeFieldName(FIELD_DURATION);
   generator.writeNumber(media.duration);
   generator.writeFieldName(FIELD_SIZE);
   generator.writeNumber(media.size);
   if (media.hasBitrate) {
     generator.writeFieldName(FIELD_BITRATE);
     generator.writeNumber(media.bitrate);
   }
   if (media.copyright != null) {
     generator.writeFieldName(FIELD_COPYRIGHT);
     generator.writeString(media.copyright);
   }
   generator.writeFieldName(FIELD_PERSONS);
   generator.writeStartArray();
   for (String person : media.persons) {
     generator.writeString(person);
   }
   generator.writeEndArray();
   generator.writeEndObject();
 }
Ejemplo n.º 4
0
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String sid = req.getParameter("sid");
    String gameId = req.getParameter("game");

    HanabiUser user = s.getUserBySession(sid);
    HanabiGame game = s.getGame(gameId);

    JsonGenerator out = new JsonFactory().createJsonGenerator(resp.getOutputStream());

    out.writeStartObject();
    out.writeStringField("id", gameId);
    out.writeStringField("nextEvent", Integer.toString(game.events.getNextId()));
    out.writeNumberField("drawPile", game.drawPile.size());
    out.writeNumberField("hintsLeft", game.hintsLeft);
    out.writeNumberField("errorsMade", game.errorsMade);
    out.writeNumberField("turnNumber", game.turn);
    out.writeStringField("activePlayer", Integer.toString(game.activeSeat));

    out.writeFieldName("seats");
    out.writeStartArray();
    for (int i = 0; i < game.seats.size(); i++) {
      HanabiGame.Seat seat = game.seats.get(i);
      out.writeStartObject();
      out.writeStringField("seat", Integer.toString(i));
      out.writeStringField("playerName", seat.user.name);
      if (seat.user == user) {
        out.writeBooleanField("isYou", true);
      }
      out.writeFieldName("hand");
      out.writeStartArray();
      for (HanabiGame.Card c : seat.hand) {
        if (seat.user == user) {
          out.writeString("unknown");
        } else {
          out.writeString(c.toString());
        }
      }
      out.writeEndArray();
      out.writeEndObject();
    }
    out.writeEndArray(); // end seats

    out.writeFieldName("piles");
    out.writeStartObject();
    for (int i = 0; i < HanabiGame.SUIT_COUNT; i++) {
      HanabiGame.Card c = game.getPileTopCard(i);
      if (c != null) {
        out.writeStringField(c.getSuitName(), c.toString());
      }
    }
    out.writeEndObject(); // end piles

    out.writeFieldName("discards");
    out.writeStartArray();
    for (HanabiGame.Card c : game.discards) {
      out.writeString(c.toString());
    }
    out.writeEndArray();

    out.writeFieldName("hints");
    out.writeStartArray();
    for (HanabiGame.Hint h : game.hints) {
      out.writeStartObject();
      out.writeNumberField("turn", h.whenGiven);
      out.writeStringField("from", Integer.toString(h.from));
      out.writeStringField("to", Integer.toString(h.to));
      out.writeStringField("hintType", h.type.name());
      out.writeStringField("hint", h.getHintString());

      HanabiGame.Seat targetSeat = game.seats.get(h.to);
      out.writeFieldName("applies");
      out.writeStartArray();
      for (int slot = 0; slot < targetSeat.hand.size(); slot++) {
        if (targetSeat.whenReceived.get(slot).intValue() > h.whenGiven) {
          out.writeString("");
        } else if (h.affirms(targetSeat.hand.get(slot))) {
          out.writeString("Y");
        } else {
          out.writeString("N");
        }
      }
      out.writeEndArray();
      out.writeEndObject();
    }
    out.writeEndArray(); // end hints

    out.writeEndObject(); // end game
    out.close();
  }
 @Override
 public void serialize(JsonGenerator jgen, SerializerProvider provider)
     throws IOException, JsonProcessingException {
   jgen.writeString("foo");
 }
 @Override
 public void serialize(Enum value, JsonGenerator jgen, SerializerProvider provider)
     throws IOException {
   jgen.writeString(value.name().toLowerCase());
 }
 @Override
 public void serialize(JsonGenerator jgen, SerializerProvider provider)
     throws IOException, JsonGenerationException {
   jgen.writeString(_desc);
 }