You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LfsGson.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C) 2017, David Pursehouse <david.pursehouse@gmail.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lfs.server.internal;
  11. import java.io.Reader;
  12. import com.google.gson.FieldNamingPolicy;
  13. import com.google.gson.Gson;
  14. import com.google.gson.GsonBuilder;
  15. import com.google.gson.JsonIOException;
  16. import com.google.gson.JsonSyntaxException;
  17. /**
  18. * Wrapper for {@link com.google.gson.Gson} used by LFS servlets.
  19. */
  20. public class LfsGson {
  21. private static final Gson gson = new GsonBuilder()
  22. .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
  23. .disableHtmlEscaping()
  24. .create();
  25. /**
  26. * Wrapper class only used for serialization of error messages.
  27. */
  28. static class Error {
  29. String message;
  30. Error(String m) {
  31. this.message = m;
  32. }
  33. }
  34. /**
  35. * Serializes the specified object into its equivalent Json representation.
  36. *
  37. * @param src
  38. * the object for which Json representation is to be created. If
  39. * this is a String, it is wrapped in an instance of
  40. * {@link org.eclipse.jgit.lfs.server.internal.LfsGson.Error}.
  41. * @param writer
  42. * Writer to which the Json representation needs to be written
  43. * @throws com.google.gson.JsonIOException
  44. * if there was a problem writing to the writer
  45. * @see Gson#toJson(Object, Appendable)
  46. */
  47. public static void toJson(Object src, Appendable writer)
  48. throws JsonIOException {
  49. if (src instanceof String) {
  50. gson.toJson(new Error((String) src), writer);
  51. } else {
  52. gson.toJson(src, writer);
  53. }
  54. }
  55. /**
  56. * Deserializes the Json read from the specified reader into an object of
  57. * the specified type.
  58. *
  59. * @param json
  60. * reader producing json from which the object is to be
  61. * deserialized
  62. * @param classOfT
  63. * specified type to deserialize
  64. * @return an Object of type T
  65. * @throws com.google.gson.JsonIOException
  66. * if there was a problem reading from the Reader
  67. * @throws com.google.gson.JsonSyntaxException
  68. * if json is not a valid representation for an object of type
  69. * @see Gson#fromJson(Reader, java.lang.reflect.Type)
  70. * @param <T>
  71. * a T object.
  72. */
  73. public static <T> T fromJson(Reader json, Class<T> classOfT)
  74. throws JsonSyntaxException, JsonIOException {
  75. return gson.fromJson(json, classOfT);
  76. }
  77. }