PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/google-http-client/src/main/java/com/google/api/client/http/json/JsonHttpContent.java

https://code.google.com/p/google-http-java-client/
Java | 98 lines | 34 code | 11 blank | 53 comment | 0 complexity | 9f6b43689c1a2123275cc5e040773f50 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright (c) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the License
  10. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. * or implied. See the License for the specific language governing permissions and limitations under
  12. * the License.
  13. */
  14. package com.google.api.client.http.json;
  15. import com.google.api.client.http.AbstractHttpContent;
  16. import com.google.api.client.http.HttpMediaType;
  17. import com.google.api.client.json.Json;
  18. import com.google.api.client.json.JsonFactory;
  19. import com.google.api.client.json.JsonGenerator;
  20. import com.google.common.base.Preconditions;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. /**
  24. * Serializes JSON HTTP content based on the data key/value mapping object for an item.
  25. *
  26. * <p>
  27. * Sample usage:
  28. * </p>
  29. *
  30. * <pre>
  31. * <code>
  32. static void setContent(HttpRequest request, Object data) {
  33. request.setContent(new JsonHttpContent(new JacksonFactory(), data));
  34. }
  35. * </code>
  36. * </pre>
  37. *
  38. * <p>
  39. * Implementation is not thread-safe.
  40. * </p>
  41. *
  42. * @since 1.0
  43. * @author Yaniv Inbar
  44. */
  45. public class JsonHttpContent extends AbstractHttpContent {
  46. // TODO(yanivi): ability to annotate fields as only needed for POST?
  47. /** JSON key name/value data. */
  48. private final Object data;
  49. /** JSON factory. */
  50. private final JsonFactory jsonFactory;
  51. /**
  52. * @param jsonFactory JSON factory to use
  53. * @param data JSON key name/value data
  54. * @since 1.5
  55. */
  56. public JsonHttpContent(JsonFactory jsonFactory, Object data) {
  57. super(Json.MEDIA_TYPE);
  58. this.jsonFactory = Preconditions.checkNotNull(jsonFactory);
  59. this.data = Preconditions.checkNotNull(data);
  60. }
  61. public void writeTo(OutputStream out) throws IOException {
  62. JsonGenerator generator = jsonFactory.createJsonGenerator(out, getCharset());
  63. generator.serialize(data);
  64. generator.flush();
  65. }
  66. @Override
  67. public JsonHttpContent setMediaType(HttpMediaType mediaType) {
  68. super.setMediaType(mediaType);
  69. return this;
  70. }
  71. /**
  72. * Returns the JSON key name/value data.
  73. *
  74. * @since 1.5
  75. */
  76. public final Object getData() {
  77. return data;
  78. }
  79. /**
  80. * Returns the JSON factory.
  81. *
  82. * @since 1.5
  83. */
  84. public final JsonFactory getJsonFactory() {
  85. return jsonFactory;
  86. }
  87. }