Ignore:
Timestamp:
2017-12-23T02:40:43+01:00 (6 years ago)
Author:
Don-vip
Message:

see #15682 - upgrade to JSR 374 (JSON Processing) API 1.1.2

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/glassfish/json/JsonParserImpl.java

    r6756 r13231  
    22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    33 *
    4  * Copyright (c) 2012-2013 Oracle and/or its affiliates. All rights reserved.
     4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
    55 *
    66 * The contents of this file are subject to the terms of either the GNU
     
    99 * may not use this file except in compliance with the License.  You can
    1010 * obtain a copy of the License at
    11  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    12  * or packager/legal/LICENSE.txt.  See the License for the specific
     11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1
     12 * or LICENSE.txt.  See the License for the specific
    1313 * language governing permissions and limitations under the License.
    1414 *
    1515 * When distributing the software, include this License Header Notice in each
    16  * file and include the License file at packager/legal/LICENSE.txt.
     16 * file and include the License file at LICENSE.txt.
    1717 *
    1818 * GPL Classpath Exception:
     
    4141package org.glassfish.json;
    4242
    43 import javax.json.*;
     43import java.io.IOException;
     44import java.io.InputStream;
     45import java.io.InputStreamReader;
     46import java.io.Reader;
     47import java.math.BigDecimal;
     48import java.nio.charset.Charset;
     49import java.util.AbstractMap;
     50import java.util.Map;
     51import java.util.NoSuchElementException;
     52import java.util.Spliterator;
     53import java.util.Spliterators;
     54import java.util.function.Consumer;
     55import java.util.stream.Stream;
     56import java.util.stream.StreamSupport;
     57
     58import javax.json.JsonArray;
     59import javax.json.JsonArrayBuilder;
     60import javax.json.JsonException;
     61import javax.json.JsonObject;
     62import javax.json.JsonObjectBuilder;
     63import javax.json.JsonValue;
    4464import javax.json.stream.JsonLocation;
    4565import javax.json.stream.JsonParser;
     66import javax.json.stream.JsonParser.Event;
    4667import javax.json.stream.JsonParsingException;
    47 import java.io.*;
    48 import java.math.BigDecimal;
    49 import java.nio.charset.Charset;
    50 import java.util.*;
    5168
    5269import org.glassfish.json.JsonTokenizer.JsonToken;
     
    5875 *
    5976 * @author Jitendra Kotamraju
     77 * @author Kin-man Chung
    6078 */
    6179public class JsonParserImpl implements JsonParser {
    6280
     81    private final BufferPool bufferPool;
    6382    private Context currentContext = new NoneContext();
    6483    private Event currentEvent;
    6584
    6685    private final Stack stack = new Stack();
    67     private final StateIterator stateIterator;
    6886    private final JsonTokenizer tokenizer;
    6987
    7088    public JsonParserImpl(Reader reader, BufferPool bufferPool) {
     89        this.bufferPool = bufferPool;
    7190        tokenizer = new JsonTokenizer(reader, bufferPool);
    72         stateIterator = new StateIterator();
    7391    }
    7492
    7593    public JsonParserImpl(InputStream in, BufferPool bufferPool) {
     94        this.bufferPool = bufferPool;
    7695        UnicodeDetectingInputStream uin = new UnicodeDetectingInputStream(in);
    7796        tokenizer = new JsonTokenizer(new InputStreamReader(uin, uin.getCharset()), bufferPool);
    78         stateIterator = new StateIterator();
    7997    }
    8098
    8199    public JsonParserImpl(InputStream in, Charset encoding, BufferPool bufferPool) {
     100        this.bufferPool = bufferPool;
    82101        tokenizer = new JsonTokenizer(new InputStreamReader(in, encoding), bufferPool);
    83         stateIterator = new StateIterator();
    84     }
    85 
     102    }
     103
     104    @Override
    86105    public String getString() {
    87106        if (currentEvent == Event.KEY_NAME || currentEvent == Event.VALUE_STRING
     
    115134    }
    116135
     136    boolean isDefinitelyLong() {
     137        return tokenizer.isDefinitelyLong();
     138    }
     139
    117140    @Override
    118141    public long getLong() {
     
    121144                    JsonMessages.PARSER_GETLONG_ERR(currentEvent));
    122145        }
    123         return tokenizer.getBigDecimal().longValue();
     146        return tokenizer.getLong();
    124147    }
    125148
     
    134157
    135158    @Override
     159    public JsonArray getArray() {
     160        if (currentEvent != Event.START_ARRAY) {
     161            throw new IllegalStateException(
     162                JsonMessages.PARSER_GETARRAY_ERR(currentEvent));
     163        }
     164        return getArray(new JsonArrayBuilderImpl(bufferPool));
     165    }
     166
     167    @Override
     168    public JsonObject getObject() {
     169        if (currentEvent != Event.START_OBJECT) {
     170            throw new IllegalStateException(
     171                JsonMessages.PARSER_GETOBJECT_ERR(currentEvent));
     172        }
     173        return getObject(new JsonObjectBuilderImpl(bufferPool));
     174    }
     175
     176    @Override
     177    public JsonValue getValue() {
     178        switch (currentEvent) {
     179            case START_ARRAY:
     180                return getArray(new JsonArrayBuilderImpl(bufferPool));
     181            case START_OBJECT:
     182                return getObject(new JsonObjectBuilderImpl(bufferPool));
     183            case KEY_NAME:
     184            case VALUE_STRING:
     185                return new JsonStringImpl(getString());
     186            case VALUE_NUMBER:
     187                if (isDefinitelyInt()) {
     188                    return JsonNumberImpl.getJsonNumber(getInt());
     189                } else if (isDefinitelyLong()) {
     190                    return JsonNumberImpl.getJsonNumber(getLong());
     191                }
     192                return JsonNumberImpl.getJsonNumber(getBigDecimal());
     193            case VALUE_TRUE:
     194                return JsonValue.TRUE;
     195            case VALUE_FALSE:
     196                return JsonValue.FALSE;
     197            case VALUE_NULL:
     198                return JsonValue.NULL;
     199            case END_ARRAY:
     200            case END_OBJECT:
     201            default:
     202                throw new IllegalStateException(JsonMessages.PARSER_GETVALUE_ERR(currentEvent));
     203        }
     204    }
     205
     206    @Override
     207    public Stream<JsonValue> getArrayStream() {
     208        if (currentEvent != Event.START_ARRAY) {
     209            throw new IllegalStateException(
     210                JsonMessages.PARSER_GETARRAY_ERR(currentEvent));
     211        }
     212        Spliterator<JsonValue> spliterator =
     213                new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) {
     214            @Override
     215            public Spliterator<JsonValue> trySplit() {
     216                return null;
     217            }
     218            @Override
     219            public boolean tryAdvance(Consumer<? super JsonValue> action) {
     220                if (action == null) {
     221                    throw new NullPointerException();
     222                }
     223                if (! hasNext()) {
     224                    return false;
     225                }
     226                if (next() == JsonParser.Event.END_ARRAY) {
     227                    return false;
     228                }
     229                action.accept(getValue());
     230                return true;
     231            }
     232        };
     233        return StreamSupport.stream(spliterator, false);
     234    }
     235
     236    @Override
     237    public Stream<Map.Entry<String, JsonValue>> getObjectStream() {
     238        if (currentEvent != Event.START_OBJECT) {
     239            throw new IllegalStateException(
     240                JsonMessages.PARSER_GETOBJECT_ERR(currentEvent));
     241        }
     242        Spliterator<Map.Entry<String, JsonValue>> spliterator =
     243                new Spliterators.AbstractSpliterator<Map.Entry<String, JsonValue>>(Long.MAX_VALUE, Spliterator.ORDERED) {
     244            @Override
     245            public Spliterator<Map.Entry<String,JsonValue>> trySplit() {
     246                return null;
     247            }
     248            @Override
     249            public boolean tryAdvance(Consumer<? super Map.Entry<String, JsonValue>> action) {
     250                if (action == null) {
     251                    throw new NullPointerException();
     252                }
     253                if (! hasNext()) {
     254                    return false;
     255                }
     256                JsonParser.Event e = next();
     257                if (e == JsonParser.Event.END_OBJECT) {
     258                    return false;
     259                }
     260                if (e != JsonParser.Event.KEY_NAME) {
     261                    throw new JsonException(JsonMessages.INTERNAL_ERROR());
     262                }
     263                String key = getString();
     264                if (! hasNext()) {
     265                    throw new JsonException(JsonMessages.INTERNAL_ERROR());
     266                }
     267                next();
     268                JsonValue value = getValue();
     269                action.accept(new AbstractMap.SimpleImmutableEntry<>(key, value));
     270                return true;
     271            }
     272        };
     273        return StreamSupport.stream(spliterator, false);
     274    }
     275
     276    @Override
     277    public Stream<JsonValue> getValueStream() {
     278        if (! (currentContext instanceof NoneContext)) {
     279            throw new IllegalStateException(
     280                JsonMessages.PARSER_GETVALUESTREAM_ERR());
     281        }
     282        Spliterator<JsonValue> spliterator =
     283                new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) {
     284            @Override
     285            public Spliterator<JsonValue> trySplit() {
     286                return null;
     287            }
     288            @Override
     289            public boolean tryAdvance(Consumer<? super JsonValue> action) {
     290                if (action == null) {
     291                    throw new NullPointerException();
     292                }
     293                if (! hasNext()) {
     294                    return false;
     295                }
     296                next();
     297                action.accept(getValue());
     298                return true;
     299            }
     300        };
     301        return StreamSupport.stream(spliterator, false);
     302    }
     303
     304    @Override
     305    public void skipArray() {
     306        if (currentEvent == Event.START_ARRAY) {
     307            currentContext.skip();
     308            currentContext = stack.pop();
     309        }
     310    }
     311
     312    @Override
     313    public void skipObject() {
     314        if (currentEvent == Event.START_OBJECT) {
     315            currentContext.skip();
     316            currentContext = stack.pop();
     317        }
     318    }
     319
     320    private JsonArray getArray(JsonArrayBuilder builder) {
     321        while(hasNext()) {
     322            JsonParser.Event e = next();
     323            if (e == JsonParser.Event.END_ARRAY) {
     324                return builder.build();
     325            }
     326            builder.add(getValue());
     327        }
     328        throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]");
     329    }
     330
     331    private JsonObject getObject(JsonObjectBuilder builder) {
     332        while(hasNext()) {
     333            JsonParser.Event e = next();
     334            if (e == JsonParser.Event.END_OBJECT) {
     335                return builder.build();
     336            }
     337            String key = getString();
     338            next();
     339            builder.add(key, getValue());
     340        }
     341        throw parsingException(JsonToken.EOF, "[STRING, CURLYCLOSE]");
     342    }
     343
     344    @Override
    136345    public JsonLocation getLocation() {
    137346        return tokenizer.getLocation();
     
    142351    }
    143352
     353    @Override
    144354    public boolean hasNext() {
    145         return stateIterator.hasNext();
    146     }
    147 
     355        return tokenizer.hasNextToken();
     356    }
     357
     358    @Override
    148359    public Event next() {
    149         return stateIterator.next();
    150     }
    151 
    152     private class StateIterator implements  Iterator<JsonParser.Event> {
    153 
    154         @Override
    155         public boolean hasNext() {
    156             if (stack.isEmpty() && (currentEvent == Event.END_ARRAY || currentEvent == Event.END_OBJECT)) {
    157                 JsonToken token = tokenizer.nextToken();
    158                 if (token != JsonToken.EOF) {
    159                     throw new JsonParsingException(JsonMessages.PARSER_EXPECTED_EOF(token),
    160                             getLastCharLocation());
    161                 }
    162                 return false;
    163             }
    164             return true;
    165         }
    166 
    167         @Override
    168         public JsonParser.Event next() {
    169             if (!hasNext()) {
    170                 throw new NoSuchElementException();
    171             }
    172             return currentEvent = currentContext.getNextEvent();
    173         }
    174 
    175         @Override
    176         public void remove() {
    177             throw new UnsupportedOperationException();
    178         }
    179     }
    180 
     360        if (!hasNext()) {
     361            throw new NoSuchElementException();
     362        }
     363        return currentEvent = currentContext.getNextEvent();
     364    }
     365
     366    @Override
    181367    public void close() {
    182368        try {
     
    206392        }
    207393
     394        private Context peek() {
     395            return head;
     396        }
     397
    208398        private boolean isEmpty() {
    209399            return head == null;
     
    214404        Context next;
    215405        abstract Event getNextEvent();
     406        abstract void skip();
    216407    }
    217408
     
    219410        @Override
    220411        public Event getNextEvent() {
    221             // Handle 1. {     2. [
     412            // Handle 1. {   2. [   3. value
    222413            JsonToken token = tokenizer.nextToken();
    223414            if (token == JsonToken.CURLYOPEN) {
     
    229420                currentContext = new ArrayContext();
    230421                return Event.START_ARRAY;
    231             }
    232             throw parsingException(token, "[CURLYOPEN, SQUAREOPEN]");
     422            } else if (token.isValue()) {
     423                return token.getEvent();
     424            }
     425            throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]");
     426        }
     427
     428        @Override
     429        void skip() {
     430            // no-op
    233431        }
    234432    }
     
    293491        }
    294492
     493        @Override
     494        void skip() {
     495            JsonToken token;
     496            int depth = 1;
     497            do {
     498                token = tokenizer.nextToken();
     499                switch (token) {
     500                    case CURLYCLOSE:
     501                        depth--;
     502                        break;
     503                    case CURLYOPEN:
     504                        depth++;
     505                        break;
     506                }
     507            } while (!(token == JsonToken.CURLYCLOSE && depth == 0));
     508        }
     509
    295510    }
    296511
     
    328543        }
    329544
     545        @Override
     546        void skip() {
     547            JsonToken token;
     548            int depth = 1;
     549            do {
     550                token = tokenizer.nextToken();
     551                switch (token) {
     552                    case SQUARECLOSE:
     553                        depth--;
     554                        break;
     555                    case SQUAREOPEN:
     556                        depth++;
     557                        break;
     558                }
     559            } while (!(token == JsonToken.SQUARECLOSE && depth == 0));
     560        }
    330561    }
    331562
Note: See TracChangeset for help on using the changeset viewer.