Search in sources :

Example 41 with JsonReader

use of com.google.gson.stream.JsonReader in project intellij-community by JetBrains.

the class DiffHttpService method execute.

@Override
@Nullable
public String execute(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) throws IOException {
    final List<DiffContent> contents = new ArrayList<>();
    final List<String> titles = new ArrayList<>();
    boolean focused = true;
    String windowTitle = null;
    JsonReader reader = createJsonReader(request);
    if (reader.hasNext()) {
        String fileType = null;
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("fileType")) {
                fileType = reader.nextString();
            } else if (name.equals("focused")) {
                focused = reader.nextBoolean();
            } else if (name.equals("windowTitle")) {
                windowTitle = StringUtil.nullize(reader.nextString(), true);
            } else if (name.equals("contents")) {
                String error = readContent(reader, contents, titles, fileType);
                if (error != null) {
                    return error;
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }
    if (contents.isEmpty()) {
        return "Empty request";
    }
    Project project = getLastFocusedOrOpenedProject();
    if (project == null) {
        // Argument for @NotNull parameter 'project' of com/intellij/openapi/components/ServiceManager.getService must not be null
        project = ProjectManager.getInstance().getDefaultProject();
    }
    final boolean finalFocused = focused;
    final String finalWindowTitle = windowTitle;
    final Project finalProject = project;
    ApplicationManager.getApplication().invokeLater(() -> {
        if (finalFocused) {
            ProjectUtil.focusProjectWindow(finalProject, true);
        }
        DiffManager.getInstance().showDiff(finalProject, new SimpleDiffRequest(StringUtil.notNullize(finalWindowTitle, "Diff Service"), contents, titles));
    }, project.getDisposed());
    sendOk(request, context);
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) ArrayList(java.util.ArrayList) JsonReader(com.google.gson.stream.JsonReader) DiffContent(com.intellij.diff.contents.DiffContent) Nullable(org.jetbrains.annotations.Nullable)

Example 42 with JsonReader

use of com.google.gson.stream.JsonReader in project cloudstack by apache.

the class Request method getCommands.

public Command[] getCommands() {
    if (_cmds == null) {
        try {
            StringReader reader = new StringReader(_content);
            JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);
            _cmds = s_gson.fromJson(jsonReader, (Type) Command[].class);
        } catch (RuntimeException e) {
            s_logger.error("Caught problem with " + _content, e);
            throw e;
        }
    }
    return _cmds;
}
Also used : Type(java.lang.reflect.Type) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Command(com.cloud.agent.api.Command) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Example 43 with JsonReader

use of com.google.gson.stream.JsonReader in project gce2retrofit by chiuki.

the class Generator method generate.

public static void generate(Reader discoveryReader, WriterFactory writerFactory, Map<String, String> classMap, EnumSet<MethodType> methodTypes, Map<String, String> packageMap) throws IOException, URISyntaxException {
    JsonReader jsonReader = new JsonReader(discoveryReader);
    Discovery discovery = gson.fromJson(jsonReader, Discovery.class);
    String packageName = StringUtil.getPackageName(discovery.baseUrl);
    if (packageName == null || packageName.isEmpty()) {
        packageName = StringUtil.getPackageName(discovery.rootUrl);
    }
    String modelPackageName = packageName + ".model";
    for (Entry<String, JsonElement> entry : discovery.schemas.entrySet()) {
        generateModel(writerFactory, modelPackageName, entry.getValue().getAsJsonObject(), classMap, packageMap);
    }
    if (discovery.resources != null) {
        generateInterfaceFromResources(writerFactory, packageName, "", discovery.resources, methodTypes, packageMap);
    }
    if (discovery.name != null && discovery.methods != null) {
        generateInterface(writerFactory, packageName, discovery.name, discovery.methods, methodTypes, packageMap);
    }
}
Also used : JsonElement(com.google.gson.JsonElement) JsonReader(com.google.gson.stream.JsonReader)

Example 44 with JsonReader

use of com.google.gson.stream.JsonReader in project cdap by caskdata.

the class JsonStructuredRecordDatumReader method decodeMap.

@Override
protected Map<?, ?> decodeMap(Decoder decoder, Schema keySchema, Schema valueSchema) throws IOException {
    if (!keySchema.isCompatible(Schema.of(Schema.Type.STRING))) {
        throw new IOException("Complex key type in maps are not supported: " + keySchema);
    }
    Map<Object, Object> result = new HashMap<>();
    JsonReader jsonReader = getJsonReader(decoder);
    jsonReader.beginObject();
    while (jsonReader.peek() != JsonToken.END_OBJECT) {
        Object key = convertKey(jsonReader.nextName(), keySchema.getType());
        result.put(key, decode(decoder, valueSchema));
    }
    jsonReader.endObject();
    return result;
}
Also used : HashMap(java.util.HashMap) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException)

Example 45 with JsonReader

use of com.google.gson.stream.JsonReader in project cdap by caskdata.

the class JsonStructuredRecordDatumReader method decodeUnion.

@Override
protected Object decodeUnion(Decoder decoder, Schema unionSchema) throws IOException {
    JsonReader jsonReader = getJsonReader(decoder);
    JsonToken token = jsonReader.peek();
    // Based on the token to guess the schema
    for (Schema schema : unionSchema.getUnionSchemas()) {
        if (SCHEMA_TO_JSON_TYPE.get(schema.getType()) == token) {
            return decode(decoder, schema);
        }
    }
    throw new IOException(String.format("No matching schema found for union type: %s for token: %s", unionSchema, token));
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) JsonReader(com.google.gson.stream.JsonReader) JsonToken(com.google.gson.stream.JsonToken) IOException(java.io.IOException)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)100 StringReader (java.io.StringReader)38 JsonElement (com.google.gson.JsonElement)31 IOException (java.io.IOException)20 JsonObject (com.google.gson.JsonObject)19 Test (org.junit.Test)19 InputStreamReader (java.io.InputStreamReader)18 JsonParser (com.google.gson.JsonParser)12 HumanReadableException (com.facebook.buck.util.HumanReadableException)10 Gson (com.google.gson.Gson)10 JsonWriter (com.google.gson.stream.JsonWriter)10 TypeToken (com.google.gson.reflect.TypeToken)8 Map (java.util.Map)8 JsonToken (com.google.gson.stream.JsonToken)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 StringWriter (java.io.StringWriter)5 Type (java.lang.reflect.Type)5 ArrayList (java.util.ArrayList)5 GsonBuilder (com.google.gson.GsonBuilder)4