All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.fastjson2.JSONPathSingleIndex Maven / Gradle / Ivy

Go to download

Fastjson is a JSON processor (JSON parser + JSON generator) written in Java

There is a newer version: 2.0.53.android8
Show newest version
package com.alibaba.fastjson2;

import java.util.*;

final class JSONPathSingleIndex
        extends JSONPathSingle {
    final JSONPathSegmentIndex segment;
    final int index;

    public JSONPathSingleIndex(String path, JSONPathSegmentIndex segment, Feature... features) {
        super(segment, path, features);
        this.segment = segment;
        this.index = segment.index;
    }

    @Override
    public Object eval(Object object) {
        if (object == null) {
            return null;
        }

        if (object instanceof java.util.List) {
            Object value = null;
            List list = (List) object;
            if (index < list.size()) {
                value = list.get(index);
            }
            return value;
        }

        Context context = new Context(this, null, segment, null, 0);
        context.root = object;
        segment.eval(context);
        return context.value;
    }

    @Override
    public Object extract(JSONReader jsonReader) {
        if (jsonReader.nextIfNull()) {
            return null;
        }

        int max = jsonReader.startArray();
        if (jsonReader.jsonb && index >= max) {
            return null;
        }

        if ((!jsonReader.jsonb) && jsonReader.nextIfArrayEnd()) {
            return null;
        }

        for (int i = 0; i < index && i < max; i++) {
            jsonReader.skipValue();
            if ((!jsonReader.jsonb) && jsonReader.nextIfArrayEnd()) {
                return null;
            }
        }
        return jsonReader.readAny();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy