GeezerVison is showing github.com — Changed to an enumset for output options for greater flexibility · jonfhancock/JsonToJava@0363e6d
File treeExpand file treeCollapse file tree Open diff view settings src/com/jonfhancock/jsontojava
JsonToJava-CLI/src/com/jsontojava/cli JsonToJava-Lib/src/com/jsontojava
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: JsonToJava-AppEngine/src/com/jonfhancock/jsontojava/JsonToJavaServlet.java +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | import javax.servlet.http.HttpServletResponse; | 9 | 9 |
| 10 | 10 | import com.jsontojava.JsonToJava; | | 11 | +import com.jsontojava.OutputOption; | 11 | 12 |
| 12 | 13 | @SuppressWarnings("serial") | 13 | 14 | public class JsonToJavaServlet extends HttpServlet { | 14 | 15 | public void doGet(HttpServletRequest req, HttpServletResponse resp) | 15 | 16 | throws IOException { | 16 | 17 |
| 17 | 18 | JsonToJava jsonToJava = new JsonToJava(); | 18 | | - boolean parcel = req.getParameter("p") != null | 19 | | - && req.getParameter("p").equalsIgnoreCase("true"); | 20 | | - boolean gson = req.getParameter("g") != null | 21 | | - && req.getParameter("g").equalsIgnoreCase("true"); | 22 | 19 | jsonToJava.setUrl(req.getParameter("url")); | 23 | 20 | jsonToJava.setPackage(req.getParameter("package")); | 24 | 21 | jsonToJava.setBaseType(req.getParameter("class")); | 25 | | - jsonToJava.useGsonAnnotations(gson); | 26 | | - jsonToJava.useParcelable(parcel); | 27 | | - | | 22 | + String[] options = req.getParameterValues("options"); | | 23 | + if(options != null){ | | 24 | + for(String option:options){ | | 25 | + jsonToJava.addOutputOption(OutputOption.valueOf(option)); | | 26 | + } | | 27 | + } | | 28 | + System.out.println(options); | 28 | 29 | jsonToJava.fetchJson(); | 29 | 30 | ByteArrayOutputStream out = (ByteArrayOutputStream) jsonToJava | 30 | 31 | .outputZipFile(new ByteArrayOutputStream()); | |
Collapse file Copy file name to clipboard +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/com |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboardExpand all lines: JsonToJava-AppEngine/war/index.html +8-17Lines changed: 8 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,9 +73,9 @@ <h2 class="form-signin-heading">JSON To Java</h2> | 73 | 73 | name | 74 | 74 | </label> | 75 | 75 | </p> | 76 | | - <label class="checkbox"> <input type="checkbox" name="p" value="true"> | | 76 | + <label class="checkbox"> <input type="checkbox" name="options" value="PARCELABLE"> | 77 | 77 | Implement Parcelable | 78 | | - </label> <label class="checkbox"> <input type="checkbox" name="p" value="true"> | | 78 | + </label> <label class="checkbox"> <input type="checkbox" name="options" value="GSON"> | 79 | 79 | Include Gson Annotations | 80 | 80 | </label> | 81 | 81 | <button class="btn btn-large btn-primary" id="download" type="submit">Download zip</button> | @@ -90,21 +90,12 @@ <h2 class="form-signin-heading">JSON To Java</h2> | 90 | 90 | <script src="/assets/js/jquery.js"></script> | 91 | 91 | <script src="/assets/js/bootstrap.min.js"></script> | 92 | 92 | <script> | 93 | | -// $( document ).ready(function() { | 94 | | -// $("#generate").submit(function(event){ | 95 | | -// generate(); | 96 | | -// event.preventDefault(); | 97 | | -// return false; | 98 | | -// }); | 99 | | -// }); | 100 | | -// function generate(){ | 101 | | -// var formData = $("#generate").serialize(); | 102 | | - | 103 | | -// $.get( "jsontojava",formData, function( data ) { | 104 | | -// $( ".result" ).html( data ); | 105 | | -// alert( "Load was performed." ); | 106 | | -// }); | 107 | | -// } | | 93 | + $( document ).ready(function() { | | 94 | + $('#generate').submit (function() { return formValidated(); }); | | 95 | + }); | | 96 | + function formValidated(){ | | 97 | + return $("input[name=url]").val().length > 0 && $("input[name=package]").val().length > 0 && $("input[name=class]").val().length > 0; | | 98 | + } | 108 | 99 | </script> | 109 | 100 | </body> | 110 | 101 | </html> |
Collapse file Copy file name to clipboardExpand all lines: JsonToJava-CLI/src/com/jsontojava/cli/Main.java +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | import org.apache.commons.cli.ParseException; | 16 | 16 |
| 17 | 17 | import com.jsontojava.JsonToJava; | | 18 | +import com.jsontojava.OutputOption; | 18 | 19 |
| 19 | 20 | public class Main { | 20 | 21 | private static final String OPTION_PACKAGE = "package"; | @@ -39,8 +40,12 @@ public static void main(String[] args) throws IOException, ParseException { | 39 | 40 | jsonToJava.setUrl(cmd.getOptionValue(OPTION_URL)); | 40 | 41 | jsonToJava.setPackage(cmd.getOptionValue(OPTION_PACKAGE)); | 41 | 42 | jsonToJava.setBaseType(cmd.getOptionValue(OPTION_ROOT)); | 42 | | - jsonToJava.useGsonAnnotations(cmd.hasOption(OPTION_GSON)); | 43 | | - jsonToJava.useParcelable(cmd.hasOption(OPTION_PARCELABLE)); | | 43 | + if(cmd.hasOption(OPTION_GSON)){ | | 44 | + jsonToJava.addOutputOption(OutputOption.GSON); | | 45 | + } | | 46 | + if(cmd.hasOption(OPTION_PARCELABLE)){ | | 47 | + jsonToJava.addOutputOption(OutputOption.PARCELABLE); | | 48 | + } | 44 | 49 |
| 45 | 50 | jsonToJava.fetchJson(); | 46 | 51 | File zipFile = new File(jsonToJava.getPackage() + ".zip"); | |
Collapse file Copy file name to clipboardExpand all lines: JsonToJava-Lib/src/com/jsontojava/JsonToJava.java +6-9Lines changed: 6 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | import java.io.InputStream; | 5 | 5 | import java.io.OutputStream; | 6 | 6 | import java.net.URL; | | 7 | +import java.util.EnumSet; | 7 | 8 | import java.util.HashMap; | 8 | 9 | import java.util.Map; | 9 | 10 | import java.util.zip.ZipEntry; | @@ -27,12 +28,12 @@ public class JsonToJava { | 27 | 28 | private String mPackage; | 28 | 29 | private String mBaseType; | 29 | 30 | private Map<String, NewType> mTypes; | 30 | | - private boolean mParcel; | 31 | | - private boolean mGsonAnnotations; | | 31 | + private EnumSet<OutputOption> mOutputOptions; | 32 | 32 |
| 33 | 33 | public JsonToJava() { | 34 | 34 | mInflector = new Inflector(); | 35 | 35 | mTypes = new HashMap<String, NewType>(); | | 36 | + mOutputOptions = EnumSet.noneOf(OutputOption.class); | 36 | 37 |
| 37 | 38 | } | 38 | 39 |
| @@ -74,7 +75,7 @@ public OutputStream outputZipFile(OutputStream outputStream) throws IOException{ | 74 | 75 | ZipEntry e = new ZipEntry(path+ fileSeparater + className + FILE_EXTENSION_JAVA); | 75 | 76 | out.putNextEntry(e); | 76 | 77 | // File classFile = new File(dir, className + FILE_EXTENSION_JAVA); | 77 | | - IOUtils.write(type.toString(mParcel,mGsonAnnotations,this), out); | | 78 | + IOUtils.write(type.toPojoString(mOutputOptions,this), out); | 78 | 79 | out.closeEntry(); | 79 | 80 | // IOUtils.write(classFile, ); | 80 | 81 | System.out.println("Created " + className + FILE_EXTENSION_JAVA); | @@ -125,12 +126,8 @@ public void setTypes(Map<String, NewType> types) { | 125 | 126 | mTypes = types; | 126 | 127 | } | 127 | 128 |
| 128 | | - public void useParcelable(boolean useParcelable) { | 129 | | - mParcel = useParcelable; | 130 | | - } | 131 | | - | 132 | | - public void useGsonAnnotations(boolean useGson) { | 133 | | - mGsonAnnotations = useGson; | | 129 | + public void addOutputOption(OutputOption option){ | | 130 | + mOutputOptions.add(option); | 134 | 131 | } | 135 | 132 |
| 136 | 133 | private Object getJsonFromUrl(String url) throws IOException { | |
Collapse file Copy file name to clipboardExpand all lines: JsonToJava-Lib/src/com/jsontojava/NewType.java +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package com.jsontojava; | 2 | 2 |
| | 3 | +import java.util.EnumSet; | 3 | 4 | import java.util.HashSet; | 4 | 5 | import java.util.Map; | 5 | 6 | import java.util.Set; | @@ -81,18 +82,18 @@ public NewType build() { | 81 | 82 | } | 82 | 83 |
| 83 | 84 | public String toString() { | 84 | | - return toString(false, false,null); | | 85 | + return toPojoString(EnumSet.noneOf(OutputOption.class),null); | 85 | 86 | } | 86 | 87 |
| 87 | | - public String toString(boolean parcelable, boolean gson,JsonToJava jsonToJava) { | 88 | | - if (parcelable) { | | 88 | + public String toPojoString(EnumSet<OutputOption> options,JsonToJava jsonToJava) { | | 89 | + if (options.contains(OutputOption.PARCELABLE)) { | 89 | 90 | imports.add(IMPORT_ANDROID_OS_PARCEL); | 90 | 91 | imports.add(IMPORT_ANDROID_OS_PARCELABLE); | 91 | 92 | if (imports.contains(IMPORT_JAVA_UTIL_LIST)) { | 92 | 93 | imports.add(IMPORT_JAVA_UIIL_ARRAYLIST); | 93 | 94 | } | 94 | 95 | } | 95 | | - if (gson) { | | 96 | + if (options.contains(OutputOption.GSON)) { | 96 | 97 | imports.add(IMPORT_GSON_SERIALIZED_NAME); | 97 | 98 | } | 98 | 99 | StringBuilder sBuilder = new StringBuilder(); | @@ -103,7 +104,7 @@ public String toString(boolean parcelable, boolean gson,JsonToJava jsonToJava) { | 103 | 104 | } | 104 | 105 | sBuilder.append("\n\n"); | 105 | 106 | sBuilder.append("public class ").append(name); | 106 | | - if (parcelable) { | | 107 | + if (options.contains(OutputOption.PARCELABLE)) { | 107 | 108 | sBuilder.append(" implements Parcelable"); | 108 | 109 | } | 109 | 110 | sBuilder.append("{\n\n"); | @@ -121,7 +122,7 @@ public String toString(boolean parcelable, boolean gson,JsonToJava jsonToJava) { | 121 | 122 | // Insert the actual member names including the SerializedName | 122 | 123 | // annotation for Gson | 123 | 124 | for (Member member : members) { | 124 | | - if (gson) { | | 125 | + if (options.contains(OutputOption.GSON)) { | 125 | 126 | sBuilder.append(ONE_TAB+"@SerializedName(" + member.fieldName + ")\n"); | 126 | 127 | } | 127 | 128 | sBuilder.append(ONE_TAB+"private " + member.type + " " + member.name + ";").append("\n"); | @@ -139,7 +140,7 @@ public String toString(boolean parcelable, boolean gson,JsonToJava jsonToJava) { | 139 | 140 |
| 140 | 141 | sBuilder.append(generateExtraMethods()); | 141 | 142 |
| 142 | | - if (parcelable) { | | 143 | + if (options.contains(OutputOption.PARCELABLE)) { | 143 | 144 | sBuilder.append(generateParcelableCode(jsonToJava.getTypes())); | 144 | 145 | } | 145 | 146 |
| |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.jsontojava; | | 2 | + | | 3 | +public enum OutputOption { | | 4 | + PARCELABLE,GSON,JACKSON,ORG_JSON,GREEN_DAO,CONTENT_PROVIDER,RETROFIT | | 5 | + | | 6 | +} |
|