-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
save Paragraph.text as list of strings
- Loading branch information
Showing
5 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...zengine/src/main/java/org/apache/zeppelin/notebook/typeadapter/ParagraphDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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.notebook.Paragraph; | ||
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 Paragraph | ||
*/ | ||
public class ParagraphDeserializer implements JsonDeserializer<Paragraph> { | ||
private static Paragraph createParagraph() { | ||
try { | ||
Constructor<Paragraph> constructor = Paragraph.class.getDeclaredConstructor(); | ||
if (!constructor.isAccessible()) { | ||
constructor.setAccessible(true); | ||
} | ||
return constructor.newInstance(); | ||
} catch (NoSuchMethodException e) { | ||
} catch (InstantiationException e) { | ||
} catch (IllegalAccessException e) { | ||
} catch (InvocationTargetException e) { | ||
} | ||
return null; | ||
} | ||
|
||
private static Field getField(String fieldName) { | ||
Class klass = Paragraph.class; | ||
while (klass != Object.class) { | ||
try { | ||
Field field = klass.getDeclaredField(fieldName); | ||
if (!field.isAccessible()) { | ||
field.setAccessible(true); | ||
} | ||
return field; | ||
} catch (NoSuchFieldException e) { | ||
} | ||
klass = klass.getSuperclass(); | ||
} | ||
return null; | ||
} | ||
|
||
private static void extractFieldValue(JsonDeserializationContext context, JsonObject json, | ||
Paragraph dst, String fieldName) { | ||
Field field = getField(fieldName); | ||
if (field != null) { | ||
Object value = context.deserialize(json.get(fieldName), field.getType()); | ||
try { | ||
field.set(dst, value); | ||
} catch (IllegalAccessException e) { | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Paragraph deserialize(JsonElement json, Type typeOfT, | ||
JsonDeserializationContext context) throws JsonParseException { | ||
JsonObject jsonObject = (JsonObject) json; | ||
Paragraph dst = createParagraph(); | ||
if (dst == null) { | ||
return null; | ||
} | ||
extractFieldValue(context, jsonObject, dst, "title"); | ||
JsonElement textObject = jsonObject.get("text"); | ||
if (textObject != null) { | ||
if (textObject.isJsonArray()) { | ||
String[] value = context.deserialize(textObject, String[].class); | ||
dst.setText(StringUtils.join(value, "\n")); | ||
} else if (textObject.isJsonPrimitive()) { | ||
dst.setText(textObject.getAsJsonPrimitive().getAsString()); | ||
} | ||
} | ||
extractFieldValue(context, jsonObject, dst, "user"); | ||
extractFieldValue(context, jsonObject, dst, "dateUpdated"); | ||
extractFieldValue(context, jsonObject, dst, "config"); | ||
extractFieldValue(context, jsonObject, dst, "settings"); | ||
extractFieldValue(context, jsonObject, dst, "jobName"); | ||
extractFieldValue(context, jsonObject, dst, "id"); | ||
extractFieldValue(context, jsonObject, dst, "result"); | ||
extractFieldValue(context, jsonObject, dst, "dateCreated"); | ||
extractFieldValue(context, jsonObject, dst, "dateStarted"); | ||
extractFieldValue(context, jsonObject, dst, "dateFinished"); | ||
extractFieldValue(context, jsonObject, dst, "status"); | ||
extractFieldValue(context, jsonObject, dst, "errorMessage"); | ||
extractFieldValue(context, jsonObject, dst, "progressUpdateIntervalMs"); | ||
return dst; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...n-zengine/src/main/java/org/apache/zeppelin/notebook/typeadapter/ParagraphSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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.notebook.Paragraph; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Custom serializer for Paragraph | ||
*/ | ||
public class ParagraphSerializer implements JsonSerializer<Paragraph> { | ||
private static Field getField(String fieldName) { | ||
Class klass = Paragraph.class; | ||
while (klass != Object.class) { | ||
try { | ||
Field field = klass.getDeclaredField(fieldName); | ||
if (!field.isAccessible()) { | ||
field.setAccessible(true); | ||
} | ||
return field; | ||
} catch (NoSuchFieldException e) { | ||
} | ||
klass = klass.getSuperclass(); | ||
} | ||
return null; | ||
} | ||
|
||
private static void addFieldValue(JsonSerializationContext context, JsonObject json, | ||
Paragraph src, String fieldName) { | ||
Field field = getField(fieldName); | ||
if (field != null) { | ||
try { | ||
Object value = field.get(src); | ||
if (value != null) { | ||
json.add(fieldName, context.serialize(value)); | ||
} | ||
} catch (IllegalAccessException e) { | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(Paragraph src, Type typeOfSrc, | ||
JsonSerializationContext context) { | ||
JsonObject json = new JsonObject(); | ||
addFieldValue(context, json, src, "title"); | ||
String text = src.getText(); | ||
if (text != null) { | ||
json.add("text", context.serialize(text.split("\n"))); | ||
} | ||
addFieldValue(context, json, src, "user"); | ||
addFieldValue(context, json, src, "dateUpdated"); | ||
addFieldValue(context, json, src, "config"); | ||
addFieldValue(context, json, src, "settings"); | ||
addFieldValue(context, json, src, "jobName"); | ||
addFieldValue(context, json, src, "id"); | ||
addFieldValue(context, json, src, "result"); | ||
addFieldValue(context, json, src, "dateCreated"); | ||
addFieldValue(context, json, src, "dateStarted"); | ||
addFieldValue(context, json, src, "dateFinished"); | ||
addFieldValue(context, json, src, "status"); | ||
addFieldValue(context, json, src, "errorMessage"); | ||
addFieldValue(context, json, src, "progressUpdateIntervalMs"); | ||
return json; | ||
} | ||
} |