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.

JsonbObjectAdapter.java 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright 2014 James Moger.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.iciql.adapter.postgresql;
  17. import com.iciql.adapter.GsonTypeAdapter;
  18. import org.postgresql.util.PGobject;
  19. import java.sql.SQLException;
  20. /**
  21. * Postgres JSONB data type adapter maps a JSONB column to a domain object using
  22. * Google GSON.
  23. *
  24. * @param <T>
  25. * @author James Moger
  26. */
  27. public abstract class JsonbObjectAdapter<T> extends GsonTypeAdapter<T> {
  28. @Override
  29. public String getDataType() {
  30. return "jsonb";
  31. }
  32. @Override
  33. public Object serialize(T value) {
  34. String json = gson().toJson(value);
  35. PGobject pg = new PGobject();
  36. pg.setType(getDataType());
  37. try {
  38. pg.setValue(json);
  39. } catch (SQLException e) {
  40. // not thrown on base PGobject
  41. }
  42. return pg;
  43. }
  44. }