Skip to content

Commit

Permalink
save InterpreterResult.msg as list of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen committed Oct 7, 2016
1 parent 2bd5fa9 commit 155a405
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
Expand Up @@ -25,10 +25,13 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.NoteInfo;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.typeadapter.DateDeserializer;
import org.apache.zeppelin.notebook.typeadapter.InterpreterResultDeserializer;
import org.apache.zeppelin.notebook.typeadapter.InterpreterResultSerializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphSerializer;
import org.apache.zeppelin.scheduler.Job;
Expand Down Expand Up @@ -126,6 +129,7 @@ private Note getNote(String noteId) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphDeserializer());
gsonBuilder.registerTypeAdapter(InterpreterResult.class, new InterpreterResultDeserializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

Expand All @@ -150,6 +154,7 @@ public void save(Note note, AuthenticationInfo subject) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphSerializer());
gsonBuilder.registerTypeAdapter(InterpreterResult.class, new InterpreterResultSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(note);

Expand Down
Expand Up @@ -35,10 +35,13 @@
import org.apache.commons.io.IOUtils;
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.NoteInfo;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.typeadapter.DateDeserializer;
import org.apache.zeppelin.notebook.typeadapter.InterpreterResultDeserializer;
import org.apache.zeppelin.notebook.typeadapter.InterpreterResultSerializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphSerializer;
import org.apache.zeppelin.scheduler.Job.Status;
Expand Down Expand Up @@ -171,6 +174,7 @@ private Note getNote(String key) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphDeserializer());
gsonBuilder.registerTypeAdapter(InterpreterResult.class, new InterpreterResultDeserializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

Expand Down Expand Up @@ -212,6 +216,7 @@ public void save(Note note, AuthenticationInfo subject) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphSerializer());
gsonBuilder.registerTypeAdapter(InterpreterResult.class, new InterpreterResultSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(note);
String key = user + "/" + "notebook" + "/" + note.getId() + "/" + "note.json";
Expand Down
Expand Up @@ -37,11 +37,14 @@
import org.apache.commons.vfs2.VFS;
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.apache.zeppelin.conf.ZeppelinConfiguration.ConfVars;
import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.zeppelin.notebook.ApplicationState;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.NoteInfo;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.typeadapter.DateDeserializer;
import org.apache.zeppelin.notebook.typeadapter.InterpreterResultDeserializer;
import org.apache.zeppelin.notebook.typeadapter.InterpreterResultSerializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphDeserializer;
import org.apache.zeppelin.notebook.typeadapter.ParagraphSerializer;
import org.apache.zeppelin.scheduler.Job.Status;
Expand Down Expand Up @@ -163,6 +166,7 @@ private Note getNote(FileObject noteDir) throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphDeserializer());
gsonBuilder.registerTypeAdapter(InterpreterResult.class, new InterpreterResultDeserializer());
gsonBuilder.registerTypeAdapter(Date.class, new DateDeserializer());
Gson gson = gsonBuilder.create();

Expand Down Expand Up @@ -225,6 +229,7 @@ public synchronized void save(Note note, AuthenticationInfo subject) throws IOEx
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.registerTypeAdapter(Paragraph.class, new ParagraphSerializer());
gsonBuilder.registerTypeAdapter(InterpreterResult.class, new InterpreterResultSerializer());
Gson gson = gsonBuilder.create();
String json = gson.toJson(note);

Expand Down
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.apache.zeppelin.notebook.typeadapter;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import org.apache.zeppelin.interpreter.InterpreterResult;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.lang.reflect.InvocationTargetException;

/**
* Custom deserializer for InterpreterResult
*/
public class InterpreterResultDeserializer implements JsonDeserializer<InterpreterResult> {
@Override
public InterpreterResult deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = (JsonObject) json;

JsonElement codeObject = jsonObject.get("code");
InterpreterResult.Code code = InterpreterResult.Code.ERROR;
if (codeObject.isJsonPrimitive()) {
code = context.deserialize(codeObject, InterpreterResult.Code.class);
}

JsonElement typeObject = jsonObject.get("type");
InterpreterResult.Type type = InterpreterResult.Type.NULL;
if (typeObject.isJsonPrimitive()) {
type = context.deserialize(typeObject, InterpreterResult.Type.class);
}

JsonElement msgObject = jsonObject.get("msg");
String msg = "";
if (msgObject != null) {
if (msgObject.isJsonArray()) {
String[] value = context.deserialize(msgObject, String[].class);
msg = StringUtils.join(value, "\n");
} else if (msgObject.isJsonPrimitive()) {
msg = msgObject.getAsJsonPrimitive().getAsString();
}
}

return new InterpreterResult(code, type, msg);
}
}
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.apache.zeppelin.notebook.typeadapter;

import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import org.apache.zeppelin.interpreter.InterpreterResult;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
* Custom serializer for InterpreterResult
*/
public class InterpreterResultSerializer implements JsonSerializer<InterpreterResult> {
@Override
public JsonElement serialize(InterpreterResult src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.add("code", context.serialize(src.code()));
json.add("type", context.serialize(src.type()));
String msg = src.message();
if (msg != null) {
json.add("msg", context.serialize(msg.split("\n")));
}
return json;
}
}

0 comments on commit 155a405

Please sign in to comment.