@Override public final byte[] serialize(MediaContent content) throws IOException { ByteArrayOutputStream baos = outputStream(content); JsonGenerator generator = constructGenerator(baos); writeMediaContent(generator, content); generator.close(); return baos.toByteArray(); }
@Override public final void serializeItems(MediaContent[] items, OutputStream out) throws IOException { JsonGenerator generator = constructGenerator(out); // JSON allows simple sequences, so: for (int i = 0, len = items.length; i < len; ++i) { writeMediaContent(generator, items[i]); } generator.close(); }
protected void writeMediaContent(JsonGenerator generator, MediaContent content) throws IOException { generator.writeStartObject(); generator.writeFieldName(FIELD_MEDIA); writeMedia(generator, content.media); generator.writeFieldName(FIELD_IMAGES); generator.writeStartArray(); for (Image i : content.images) { writeImage(generator, i); } generator.writeEndArray(); generator.writeEndObject(); }
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(); }
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(); }