Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (C) 2005-2013 Schlichtherle IT Services.
  * All rights reserved. Use is subject to license terms.
  */
 package net.java.truelicense.json.codec;
 
 import java.io.*;
import  javax.annotation.Nullable;
import  javax.annotation.concurrent.Immutable;
A codec which encodes/decodes objects to/from JSON with an ObjectMapper.

Author(s):
Christian Schlichtherle
@Immutable
public class JsonCodec implements Codec {
    @Obfuscate
    private static final String CONTENT_TYPE = "application/json";
    @Obfuscate
    private static final String CONTENT_TRANSFER_ENCODING = "8bit";
    private final ObjectMapper mapper;

    
Constructs a new JSON codec.

Parameters:
mapper the object mapper.
    public JsonCodec(final ObjectMapper mapper) {
        this. = Objects.requireNonNull(mapper);
    }

    

The implementation in the class JsonCodec returns "application/json".

See also:
RFC 4627
    @Override public String contentType() { return ; }

    

The implementation in the class JsonCodec returns "8bit".

See also:
RFC 4627
    @Override public String contentTransferEncoding() {
        return ;
    }
    @Override
    public void encode(final Sink sinkfinal @Nullable Object obj)
    throws Exception {
        final OutputStream out = sink.output();
        try { .writeValue(outobj); }
        finally { out.close(); }
    }
    @Override
    public @Nullable <T> T decode(final Source sourcefinal Type expected)
    throws Exception {
        final InputStream in = source.input();
        try { return .readValue(in.constructType(expected)); }
        finally { in.close(); }
    }
New to GrepCode? Check out our FAQ X