PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 76 lines | 43 code | 15 blank | 18 comment | 11 complexity | 02ec9ecfeb65a72106c56adb537a0617 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 FloatArraySerializer implements ObjectSerializer {
  23. public static final FloatArraySerializer instance = new FloatArraySerializer();
  24. public FloatArraySerializer(){
  25. }
  26. public final void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  27. SerializeWriter out = serializer.getWriter();
  28. if (object == null) {
  29. if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) {
  30. out.write("[]");
  31. } else {
  32. out.writeNull();
  33. }
  34. return;
  35. }
  36. float[] array = (float[]) object;
  37. int size = array.length;
  38. int end = size - 1;
  39. if (end == -1) {
  40. out.append("[]");
  41. return;
  42. }
  43. out.append('[');
  44. for (int i = 0; i < end; ++i) {
  45. float item = array[i];
  46. if (Float.isNaN(item)) {
  47. out.writeNull();
  48. } else {
  49. out.append(Float.toString(item));
  50. }
  51. out.append(',');
  52. }
  53. float item = array[end];
  54. if (Float.isNaN(item)) {
  55. out.writeNull();
  56. } else {
  57. out.append(Float.toString(item));
  58. }
  59. out.append(']');
  60. }
  61. }