/src/main/java/com/github/jsonj/bson/BsonSerializer.java

http://github.com/jillesvangurp/jsonj · Java · 85 lines · 68 code · 12 blank · 5 comment · 22 complexity · db8c47124f98f58b5ea43e13ca3953db MD5 · raw file

  1. package com.github.jsonj.bson;
  2. import com.fasterxml.jackson.core.JsonGenerationException;
  3. import com.github.jsonj.JsonArray;
  4. import com.github.jsonj.JsonElement;
  5. import com.github.jsonj.JsonObject;
  6. import com.github.jsonj.JsonPrimitive;
  7. import de.undercouch.bson4jackson.BsonFactory;
  8. import de.undercouch.bson4jackson.BsonGenerator;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.util.Map.Entry;
  12. /**
  13. * Serialize JsonElements to mongodb style bson. Note, bson is typically slightly bigger than normal json but parser
  14. * performance tends to be better.
  15. * So make sure you use it for the right reasons.
  16. */
  17. public class BsonSerializer {
  18. public static void serialize(JsonElement element, OutputStream out) throws IOException {
  19. BsonFactory bsonFactory = new BsonFactory();
  20. BsonGenerator generator = bsonFactory.createGenerator(out);
  21. if(element.isObject()) {
  22. serialize(generator, element.asObject());
  23. } else if(element.isArray()) {
  24. serialize(generator, element.asArray());
  25. } else {
  26. serialize(generator, element.asPrimitive());
  27. }
  28. generator.close();
  29. }
  30. private static void serialize(BsonGenerator generator, JsonObject object) throws JsonGenerationException, IOException {
  31. generator.writeStartObject();
  32. for(Entry<String, JsonElement> entry: object.entrySet()) {
  33. generator.writeFieldName(entry.getKey());
  34. JsonElement element = entry.getValue();
  35. if(element.isObject()) {
  36. serialize(generator, element.asObject());
  37. } else if(element.isArray()) {
  38. serialize(generator, element.asArray());
  39. } else {
  40. serialize(generator, element.asPrimitive());
  41. }
  42. }
  43. generator.writeEndObject();
  44. }
  45. private static void serialize(BsonGenerator generator, JsonArray array) throws JsonGenerationException, IOException {
  46. generator.writeStartArray();
  47. for(JsonElement element: array) {
  48. if(element.isObject()) {
  49. serialize(generator, element.asObject());
  50. } else if(element.isArray()) {
  51. serialize(generator, element.asArray());
  52. } else {
  53. serialize(generator, element.asPrimitive());
  54. }
  55. }
  56. generator.writeEndArray();
  57. }
  58. private static void serialize(BsonGenerator generator, JsonPrimitive p) throws JsonGenerationException, IOException {
  59. if(p.isBoolean()) {
  60. generator.writeBoolean(p.asBoolean());
  61. } else if(p.isNumber()) {
  62. if(p.asString().contains(".")) {
  63. generator.writeNumber(p.asDouble());
  64. } else {
  65. generator.writeNumber(p.asLong());
  66. }
  67. } else if(p.isString()) {
  68. generator.writeString(p.asString());
  69. } else {
  70. generator.writeNull();
  71. }
  72. }
  73. }