> For the complete documentation index, see [llms.txt](https://nelmm.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nelmm.gitbook.io/til/spring/project-library/customserialize.md).

# Custom Serialize

기본적인 도메인들은 자바 빈 스펙을 따르기 때문에 `BeanSerializer`에 의해서 `Serialize/Deserialize`가 가능 하다.(ObjectMapper)

하지만 Error와 같이 빈 스펙을 따르지 않아 불가능하거나 Custom Serialize를 구현하고 싶다면, `@JsonComponent`와 `JsonSerialzer<>`를 상속받아 `serialize`를 overriding 해주자.

```java
@JsonComponent
public class LongToStringSerializer extends StdSerializer<Long> {

    @Override
    public void serialize(Long val, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(val.toString());
    }
}
```

```java
@JsonComponent
public class ErrorsSerializer extends JsonSerializer<Errors> {
    @Override
    public void serialize(Errors errors, JsonGenerator gen, SerializerProvider serializerProvider ) throws IOException {
        gen.writeFieldName("errors"); //error객체 이름
        gen.writeStartArray();        //error가 여러개 일 수 있으니 Array형태
        errors.getFieldErrors().forEach( e -> {
            try{
                gen.writeStartObject();
                gen.writeStringField("field",e.getField());
                gen.writeStringField("objectName",e.getObjectName());
                gen.writeStringField("code",e.getCode());
                gen.writeStringField("defaultMessage",e.getDefaultMessage());
                Object rejectedValue = e.getRejectedValue();
                if(rejectedValue != null){
                    gen.writeStringField("rejectedValue",rejectedValue.toString());
                }
                gen.writeEndObject();
            }catch (IOException ex){
                ex.printStackTrace();
            }
        });
        errors.getGlobalErrors().forEach(e->{
            try{
                gen.writeStartObject();
                gen.writeStringField("objectName",e.getObjectName());
                gen.writeStringField("code",e.getCode());
                gen.writeStringField("defaultMessage",e.getDefaultMessage());
                gen.writeEndObject();
            }catch (IOException ex){
                ex.printStackTrace();
            }
        });
        gen.writeEndArray();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nelmm.gitbook.io/til/spring/project-library/customserialize.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
