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.

URLReference_Serializer.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.communication;
  17. import com.google.gwt.core.client.GWT;
  18. import com.vaadin.client.ApplicationConnection;
  19. import com.vaadin.client.metadata.Type;
  20. import com.vaadin.shared.communication.URLReference;
  21. import elemental.json.Json;
  22. import elemental.json.JsonObject;
  23. import elemental.json.JsonValue;
  24. public class URLReference_Serializer implements JSONSerializer<URLReference> {
  25. // setURL() -> uRL as first char becomes lower case...
  26. private static final String URL_FIELD = "uRL";
  27. @Override
  28. public URLReference deserialize(Type type, JsonValue jsonValue,
  29. ApplicationConnection connection) {
  30. TranslatedURLReference reference = GWT
  31. .create(TranslatedURLReference.class);
  32. reference.setConnection(connection);
  33. JsonObject json = (JsonObject) jsonValue;
  34. if (json.hasKey(URL_FIELD)) {
  35. JsonValue jsonURL = json.get(URL_FIELD);
  36. String url = (String) JsonDecoder.decodeValue(
  37. new Type(String.class.getName(), null), jsonURL, null,
  38. connection);
  39. reference.setURL(url);
  40. }
  41. return reference;
  42. }
  43. @Override
  44. public JsonValue serialize(URLReference value,
  45. ApplicationConnection connection) {
  46. JsonObject json = Json.createObject();
  47. // No type info required for encoding a String...
  48. json.put(URL_FIELD,
  49. JsonEncoder.encode(value.getURL(), null, connection));
  50. return json;
  51. }
  52. }