PageRenderTime 20ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/alibaba/fastjson/serializer/StringSerializer.java

https://bitbucket.org/xiejuntao/xdesktop
Java | 46 lines | 21 code | 7 blank | 18 comment | 4 complexity | 51c3fc77fbd559b2d74b3856d4e73a84 MD5 | raw file
  1. /*
  2. * Copyright 1999-2101 Alibaba Group.
  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.alibaba.fastjson.serializer;
  17. import java.io.IOException;
  18. import java.lang.reflect.Type;
  19. /**
  20. * @author wenshao<szujobs@hotmail.com>
  21. */
  22. public class StringSerializer implements ObjectSerializer {
  23. public static StringSerializer instance = new StringSerializer();
  24. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  25. write(serializer, (String) object);
  26. }
  27. public void write(JSONSerializer serializer, String value) {
  28. SerializeWriter out = serializer.getWriter();
  29. if (value == null) {
  30. if (out.isEnabled(SerializerFeature.WriteNullStringAsEmpty)) {
  31. out.writeString("");
  32. } else {
  33. out.writeNull();
  34. }
  35. return;
  36. }
  37. out.writeString(value);
  38. }
  39. }