[ZEPPELIN-2758]. NumberFormatException on importing notebook

This commit is contained in:
Jeff Zhang 2017-07-12 11:38:41 +08:00
parent 985b86e4d2
commit b51d5665bb
5 changed files with 4 additions and 197 deletions

View file

@ -1,85 +0,0 @@
/*
* 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.json;
import java.io.IOException;
import org.apache.zeppelin.socket.NotebookServer;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* Custom adapter type factory
* Modify the jsonObject before serailaization/deserialization
* Check sample implementation at {@link NotebookServer}
* @param <C> the type whose json is to be customized for serialization/deserialization
*/
public class NotebookTypeAdapterFactory<C> implements TypeAdapterFactory {
private final Class<C> customizedClass;
public NotebookTypeAdapterFactory(Class<C> customizedClass) {
this.customizedClass = customizedClass;
}
@SuppressWarnings("unchecked")
// we use a runtime check to guarantee that 'C' and 'T' are equal
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return type.getRawType() == customizedClass ? (TypeAdapter<T>) customizeTypeAdapter(gson,
(TypeToken<C>) type) : null;
}
private TypeAdapter<C> customizeTypeAdapter(Gson gson, TypeToken<C> type) {
final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<C>() {
@Override
public void write(JsonWriter out, C value) throws IOException {
JsonElement tree = delegate.toJsonTree(value);
beforeWrite(value, tree);
elementAdapter.write(out, tree);
}
@Override
public C read(JsonReader in) throws IOException {
JsonElement tree = elementAdapter.read(in);
afterRead(tree);
return delegate.fromJsonTree(tree);
}
};
}
/**
* Override this to change {@code toSerialize} before it is written to the
* outgoing JSON stream.
*/
protected void beforeWrite(C source, JsonElement toSerialize) {
}
/**
* Override this to change {@code deserialized} before it parsed into the
* application type.
*/
protected void afterRead(JsonElement deserialized) {
}
}

View file

@ -50,7 +50,6 @@ import org.apache.zeppelin.interpreter.InterpreterSetting;
import org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry;
import org.apache.zeppelin.interpreter.remote.RemoteInterpreterProcessListener;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
import org.apache.zeppelin.json.NotebookTypeAdapterFactory;
import org.apache.zeppelin.notebook.JobListenerFactory;
import org.apache.zeppelin.notebook.Folder;
import org.apache.zeppelin.notebook.Note;
@ -117,19 +116,7 @@ public class NotebookServer extends WebSocketServlet
private static final Logger LOG = LoggerFactory.getLogger(NotebookServer.class);
private static Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new NotebookTypeAdapterFactory<Paragraph>(Paragraph.class) {
@Override
protected void beforeWrite(Paragraph source, JsonElement toSerialize) {
Map<String, ParagraphRuntimeInfo> runtimeInfos = source.getRuntimeInfos();
if (runtimeInfos != null) {
JsonElement jsonTree = gson.toJsonTree(runtimeInfos);
if (toSerialize instanceof JsonObject) {
JsonObject jsonObj = (JsonObject) toSerialize;
jsonObj.add("runtimeInfos", jsonTree);
}
}
}
}).setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.registerTypeAdapter(Date.class, new NotebookImportDeserializer())
.setPrettyPrinting()
.registerTypeAdapterFactory(Input.TypeAdapterFactory).create();

View file

@ -155,7 +155,8 @@ public class NotebookServerTest extends AbstractTestRestApi {
public void testImportNotebook() throws IOException {
String msg = "{\"op\":\"IMPORT_NOTE\",\"data\":" +
"{\"note\":{\"paragraphs\": [{\"text\": \"Test " +
"paragraphs import\",\"config\":{},\"settings\":{}}]," +
"paragraphs import\"," + "\"progressUpdateIntervalMs\":500," +
"\"config\":{},\"settings\":{}}]," +
"\"name\": \"Test Zeppelin notebook import\",\"config\": " +
"{}}}}";
Message messageReceived = notebookServer.deserializeMessage(msg);

View file

@ -39,7 +39,6 @@ import org.apache.zeppelin.display.Input;
import org.apache.zeppelin.interpreter.*;
import org.apache.zeppelin.interpreter.remote.RemoteAngularObjectRegistry;
import org.apache.zeppelin.interpreter.thrift.InterpreterCompletion;
import org.apache.zeppelin.notebook.json.NotebookTypeAdapterFactory;
import org.apache.zeppelin.notebook.repo.NotebookRepo;
import org.apache.zeppelin.notebook.utility.IdHashes;
import org.apache.zeppelin.resource.ResourcePoolUtils;
@ -62,21 +61,8 @@ public class Note implements ParagraphJobListener, JsonSerializable {
private static final long serialVersionUID = 7920699076577612429L;
private static Gson gson = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapterFactory(new NotebookTypeAdapterFactory<Paragraph>(Paragraph.class) {
@Override
protected void beforeWrite(Paragraph source, JsonElement toSerialize) {
Map<String, ParagraphRuntimeInfo> runtimeInfos = source.getRuntimeInfos();
if (runtimeInfos != null) {
JsonElement jsonTree = gson.toJsonTree(runtimeInfos);
if (toSerialize instanceof JsonObject) {
JsonObject jsonObj = (JsonObject) toSerialize;
jsonObj.add("runtimeInfos", jsonTree);
}
}
}
}).setDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.registerTypeAdapter(Date.class, new NotebookImportDeserializer())
.setPrettyPrinting()
.registerTypeAdapterFactory(Input.TypeAdapterFactory).create();
// threadpool for delayed persist of note

View file

@ -1,82 +0,0 @@
/*
* 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.json;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
/**
* Custom adapter type factory
* Modify the jsonObject before serailaization/deserialization
* @param <C> the type whose json is to be customized for serialization/deserialization
*/
public class NotebookTypeAdapterFactory<C> implements TypeAdapterFactory {
private final Class<C> customizedClass;
public NotebookTypeAdapterFactory(Class<C> customizedClass) {
this.customizedClass = customizedClass;
}
@SuppressWarnings("unchecked")
// we use a runtime check to guarantee that 'C' and 'T' are equal
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return type.getRawType() == customizedClass ? (TypeAdapter<T>) customizeTypeAdapter(gson,
(TypeToken<C>) type) : null;
}
private TypeAdapter<C> customizeTypeAdapter(Gson gson, TypeToken<C> type) {
final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<C>() {
@Override
public void write(JsonWriter out, C value) throws IOException {
JsonElement tree = delegate.toJsonTree(value);
beforeWrite(value, tree);
elementAdapter.write(out, tree);
}
@Override
public C read(JsonReader in) throws IOException {
JsonElement tree = elementAdapter.read(in);
afterRead(tree);
return delegate.fromJsonTree(tree);
}
};
}
/**
* Override this to change {@code toSerialize} before it is written to the
* outgoing JSON stream.
*/
protected void beforeWrite(C source, JsonElement toSerialize) {
}
/**
* Override this to change {@code deserialized} before it parsed into the
* application type.
*/
protected void afterRead(JsonElement deserialized) {
}
}