/src/main/java/com/googlecode/protobuf/format/SmileFormat.java

http://protobuf-java-format.googlecode.com/ · Java · 81 lines · 26 code · 8 blank · 47 comment · 0 complexity · 4b7ccbc308d5400e1720b8e4a6076aef MD5 · raw file

  1. package com.googlecode.protobuf.format;
  2. /*
  3. Copyright (c) 2009, Orbitz World Wide
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. * Neither the name of the Orbitz World Wide nor the names of its contributors
  13. may be used to endorse or promote products derived from this software
  14. without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29. import java.io.OutputStream;
  30. import java.nio.charset.Charset;
  31. import org.codehaus.jackson.JsonGenerator;
  32. import org.codehaus.jackson.smile.SmileFactory;
  33. import org.codehaus.jackson.smile.SmileGenerator;
  34. import org.codehaus.jackson.smile.SmileParser;
  35. import com.google.protobuf.ExtensionRegistry;
  36. import com.google.protobuf.Message;
  37. /**
  38. * Provide ascii text parsing and formatting support for proto2 instances. The implementation
  39. * largely follows google/protobuf/text_format.cc.
  40. * <p>
  41. * (c) 2011 Neustar, Inc. All Rights Reserved.
  42. *
  43. * @author jeffrey.damick@neustar.biz Jeffrey Damick
  44. * Based on the original code by:
  45. * @author eliran.bivas@gmail.com Eliran Bivas
  46. * @author aantonov@orbitz.com Alex Antonov
  47. * <p/>
  48. * @author wenboz@google.com Wenbo Zhu
  49. * @author kenton@google.com Kenton Varda
  50. */
  51. public class SmileFormat extends JsonJacksonFormat {
  52. private static SmileFactory smileFactory = new SmileFactory();
  53. /**
  54. * Parse a text-format message from {@code input} and merge the contents into {@code builder}.
  55. * Extensions will be recognized if they are registered in {@code extensionRegistry}.
  56. * @throws IOException
  57. */
  58. public void merge(InputStream input, Charset cs,
  59. ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException {
  60. SmileParser parser = smileFactory.createJsonParser(input);
  61. merge(parser, extensionRegistry, builder);
  62. }
  63. protected JsonGenerator createGenerator(OutputStream output) throws IOException {
  64. SmileGenerator generator = smileFactory.createJsonGenerator(output);
  65. generator.enable(SmileGenerator.Feature.WRITE_HEADER);
  66. generator.enable(SmileGenerator.Feature.WRITE_END_MARKER);
  67. generator.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
  68. return generator;
  69. }
  70. }