/src/main/java/com/alibaba/fastjson/serializer/CharacterSerializer.java
Java | 45 lines | 20 code | 7 blank | 18 comment | 5 complexity | aae51979abddf9c8fb1768e634f53141 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 */
16package com.alibaba.fastjson.serializer;
17
18import java.io.IOException;
19import java.lang.reflect.Type;
20
21/**
22 * @author wenshao<szujobs@hotmail.com>
23 */
24public class CharacterSerializer implements ObjectSerializer {
25
26 public final static CharacterSerializer instance = new CharacterSerializer();
27
28 public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
29 SerializeWriter out = serializer.getWriter();
30
31 Character value = (Character) object;
32 if (value == null) {
33 out.writeString("");
34 return;
35 }
36
37 char c = value.charValue();
38 if (c == 0) {
39 out.writeString("\u0000");
40 } else {
41 out.writeString(value.toString());
42 }
43 }
44
45}