Home / Class/ JsonObjectDecoder Class — netty Architecture

JsonObjectDecoder Class — netty Architecture

Architecture documentation for the JsonObjectDecoder class in JsonObjectDecoder.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc["JsonObjectDecoder"]
  3657111e_e1a4_70c6_7a2d_2b4eb0a07e86["JsonObjectDecoder.java"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|defined in| 3657111e_e1a4_70c6_7a2d_2b4eb0a07e86
  d355f1ed_b29a_7fba_663a_40bff95a29dd["JsonObjectDecoder()"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|method| d355f1ed_b29a_7fba_663a_40bff95a29dd
  e065d464_9fe4_dd94_1292_774ad1b9001b["decode()"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|method| e065d464_9fe4_dd94_1292_774ad1b9001b
  a206e603_a6c5_b14b_dd14_e4ffe840983e["ByteBuf()"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|method| a206e603_a6c5_b14b_dd14_e4ffe840983e
  82cd9175_c986_585e_8ea2_00d1538388bd["decodeByte()"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|method| 82cd9175_c986_585e_8ea2_00d1538388bd
  df843581_4533_d2cc_abd1_ec20eb52805c["initDecoding()"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|method| df843581_4533_d2cc_abd1_ec20eb52805c
  b07845ff_606f_b96b_ccd4_54fa788281c7["reset()"]
  cc9d6d69_638d_e1e7_aa08_bd1cacf4f8cc -->|method| b07845ff_606f_b96b_ccd4_54fa788281c7

Relationship Graph

Source Code

codec-base/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java lines 44–237

public class JsonObjectDecoder extends ByteToMessageDecoder {

    private static final int ST_CORRUPTED = -1;
    private static final int ST_INIT = 0;
    private static final int ST_DECODING_NORMAL = 1;
    private static final int ST_DECODING_ARRAY_STREAM = 2;

    private int openBraces;
    private int idx;

    private int lastReaderIndex;

    private int state;
    private boolean insideString;

    private final int maxObjectLength;
    private final boolean streamArrayElements;

    public JsonObjectDecoder() {
        // 1 MB
        this(1024 * 1024);
    }

    public JsonObjectDecoder(int maxObjectLength) {
        this(maxObjectLength, false);
    }

    public JsonObjectDecoder(boolean streamArrayElements) {
        this(1024 * 1024, streamArrayElements);
    }

    /**
     * @param maxObjectLength   maximum number of bytes a JSON object/array may use (including braces and all).
     *                             Objects exceeding this length are dropped and an {@link TooLongFrameException}
     *                             is thrown.
     * @param streamArrayElements   if set to true and the "top level" JSON object is an array, each of its entries
     *                                  is passed through the pipeline individually and immediately after it was fully
     *                                  received, allowing for arrays with "infinitely" many elements.
     *
     */
    public JsonObjectDecoder(int maxObjectLength, boolean streamArrayElements) {
        this.maxObjectLength = checkPositive(maxObjectLength, "maxObjectLength");
        this.streamArrayElements = streamArrayElements;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if (state == ST_CORRUPTED) {
            in.skipBytes(in.readableBytes());
            return;
        }

        if (this.idx > in.readerIndex() && lastReaderIndex != in.readerIndex()) {
            this.idx = in.readerIndex() + (idx - lastReaderIndex);
        }

        // index of next byte to process.
        int idx = this.idx;
        int wrtIdx = in.writerIndex();

        if (wrtIdx > maxObjectLength) {
            // buffer size exceeded maxObjectLength; discarding the complete buffer.
            in.skipBytes(in.readableBytes());
            reset();
            throw new TooLongFrameException(
                            "object length exceeds " + maxObjectLength + ": " + wrtIdx + " bytes discarded");
        }

        for (/* use current idx */; idx < wrtIdx; idx++) {
            byte c = in.getByte(idx);
            if (state == ST_DECODING_NORMAL) {
                decodeByte(c, in, idx);

                // All opening braces/brackets have been closed. That's enough to conclude
                // that the JSON object/array is complete.
                if (openBraces == 0) {
                    ByteBuf json = extractObject(ctx, in, in.readerIndex(), idx + 1 - in.readerIndex());
                    if (json != null) {
                        out.add(json);
                    }

Frequently Asked Questions

What is the JsonObjectDecoder class?
JsonObjectDecoder is a class in the netty codebase, defined in codec-base/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java.
Where is JsonObjectDecoder defined?
JsonObjectDecoder is defined in codec-base/src/main/java/io/netty/handler/codec/json/JsonObjectDecoder.java at line 44.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free