Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file is distributed
  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing
  * permissions and limitations under the License.
  */
 package com.amazonaws.transform;
 
 import static com.fasterxml.jackson.core.JsonToken.END_ARRAY;
 import static com.fasterxml.jackson.core.JsonToken.END_OBJECT;
 import static com.fasterxml.jackson.core.JsonToken.FIELD_NAME;
 import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;
 import static com.fasterxml.jackson.core.JsonToken.START_OBJECT;
 
 import java.util.Map;
 
 
 public class JsonUnmarshallerContextImpl extends JsonUnmarshallerContext {

    
The current JsonToken that the private JsonParser is currently pointing to.
 
     private JsonToken currentToken;

    
A cache of the next token if it has been peeked ahead.
 
     private JsonToken nextToken;
 
     private final JsonParser jsonParser;
 
     private String currentHeader;

    
A stack of JsonFieldTokenPair objects that indicates the current state of the context. For example, if we have a JSON object: { A : { B : [ { C : { D : E } }, ] } } When the parser points to "D", the state of this stack should be (from top to bottom): [ (C, START_OBJECT), (B, START_ARRAY), (A, START_OBJECT) ]
 
     private final Stack<JsonFieldTokenPairstack = new Stack<JsonFieldTokenPair>();

    
The name of the field that is currently being parsed. This value is nulled out when the parser reaches into the object/array structure of the corresponding value, and then it will be pushed into the stack after wrapped into a JsonFieldTokenPair object with the START_OBJECT or START_ARRAY token following it. So in the same example as shown above: (1) when the parser moves from "C" to "{", (currentField, START_OBJECT) will be pushed into the stack and currentField will be set null; (2) but when it moves from "{" to "C", nothing will be pushed into the stack and only currentField will be updated from null to "D".
 
     private String currentField;

    
This string is used to cache the parent element that was just parsed, after it is removed from the stack.
 
     private String lastParsedParentElement;
 
     private Map<StringStringmetadata = new HashMap<StringString>();
 
     private final HttpResponse httpResponse;
 
 
     public JsonUnmarshallerContextImpl(JsonParser jsonParser) {
         this(jsonParsernull);
     }
 
     public JsonUnmarshallerContextImpl(JsonParser jsonParserHttpResponse httpResponse) {
         this. = jsonParser;
         this. = httpResponse;
     }
 
     @Override
     public String getHeader(String header) {
        if ( == nullreturn null;
        return .getHeaders().get(header);
    }
    @Override
    public HttpResponse getHttpResponse() {
        return ;
    }
    @Override
    public int getCurrentDepth() {
        int depth = .size();
        if ( != nulldepth++;
        return depth;
    }
    @Override
    public String readText() throws IOException {
        if (isInsideResponseHeader()) {
            return getHeader();
        }
        return readCurrentJsonTokenValue();
    }
    private String readCurrentJsonTokenValue() throws IOException {
        switch () {
        case :
            String text = .getText();
            return text;
        case return "false";
        case return "true";
        case return null;
        case :
        case :
            return .getNumberValue().toString();
        case :
            return .getText();
        default:
            throw new RuntimeException(
                    "We expected a VALUE token but got: " + );
        }
    }
    @Override
    public boolean isInsideResponseHeader() {
        return  == null &&  == null;
    }
    @Override
    public boolean isStartOfDocument() {
        return  == null || .getCurrentToken() == null;
    }
    @Override
    public boolean testExpression(String expression) {
        if (expression.equals(".")) {
            return true;
        } else {
            if ( != null) {
                return .equals(expression);
            } else {
                return (!.isEmpty())
                        && .peek().getField().equals(expression);
            }
        }
    }
    @Override
    public String getCurrentParentElement() {
        String parentElement;
        if ( != null) {
            parentElement = ;
        } else if ( !.isEmpty() ) {
            parentElement = .peek().getField();
        } else {
            parentElement = "";
        }
        return parentElement;
    }
    @Override
    public boolean testExpression(String expressionint stackDepth) {
        if (expression.equals(".")) {
            return true;
        } else {
            return testExpression(expression)
                    && stackDepth == getCurrentDepth();
        }
    }
    @Override
    public JsonToken nextToken() throws IOException {
        // Use the value from the nextToken field if
        // we've already populated it to peek ahead.
        JsonToken token = ( != null) ?
                 : .nextToken();
        this. = token;
         = null;
        updateContext();
        return token;
    }
    @Override
    public JsonToken peek() throws IOException {
        if ( != nullreturn ;
         = .nextToken();
        return ;
    }
    @Override
    public JsonParser getJsonParser() {
        return ;
    }
    @Override
    public Map<StringStringgetMetadata() {
        return ;
    }
    @Override
    public void setCurrentHeader(String currentHeader) {
        this. = currentHeader;
    }
    @Override
    public JsonToken getCurrentToken() {
        return ;
    }
    private void updateContext() throws IOException {
         = null;
        if ( == nullreturn;
        if ( ==  ||  == ) {
            if ( != null) {
                .push(new JsonFieldTokenPair());
                 = null;
            }
        } else if ( ==  ||  == ) {
            if (!.isEmpty()) {
                boolean squareBracketsMatch =  ==  && .peek().getToken() == ;
                boolean curlyBracketsMatch =  ==  && .peek().getToken() == ;
                if (squareBracketsMatch || curlyBracketsMatch) {
                     = .pop().getField();
                }
            }
             = null;
        } else if ( == ) {
            String t = .getText();
             = t;
        }
    }
    @Override
    public String toString() {
        StringBuilder stackString = new StringBuilder();
        for (JsonFieldTokenPair jsonFieldTokenPair : ) {
            stackString.append("/")
                       .append(jsonFieldTokenPair.getField());
        }
        if ( != null) {
            stackString.append("/")
                       .append();
        }
        return stackString.length() == 0 ? "/" : stackString.toString();
    }
    @Override
        return ;
    }

    
An immutable class used to indicate a JSON field value followed by a subsequent JSON token. This inner class should only be used by the private stack to indicate the current state of the context.
    private static class JsonFieldTokenPair {
        private final String field;
        private final JsonToken jsonToken;

        

Parameters:
fieldString Not null.
token Not null.
        public JsonFieldTokenPair(String fieldStringJsonToken token) {
             = fieldString;
             = token;
        }
        public String getField() {
            return ;
        }
        public JsonToken getToken() {
            return ;
        }
        public String toString() {
            return  + ": " + .asString();
        }
    }
New to GrepCode? Check out our FAQ X