Last active
August 29, 2015 14:07
-
-
Save neilellis/06fafcde56ec1918bfdd to your computer and use it in GitHub Desktop.
Convert Main style string arguments to Json. This includes using --foo bar syntax
This file contains hidden or 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
| /* | |
| * Copyright (c) 2014 Neil Ellis | |
| * | |
| * Licensed 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 me.neilellis.dollar.json; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| /** | |
| * @author <a href="http://uk.linkedin.com/in/neilellis">Neil Ellis</a> | |
| */ | |
| public class JsonUtil { | |
| public static JsonObject argsToJson(List<String> args) { | |
| JsonObject json = new JsonObject(); | |
| String key = ""; | |
| Object value = ""; | |
| for (String arg : args) { | |
| if (arg.startsWith("-")) { | |
| if (!key.isEmpty()) { | |
| if(value instanceof String && ((String) value).isEmpty()) { | |
| value=true; | |
| } | |
| json.putValue(key, value); | |
| } | |
| key = arg.replaceAll("^\\-+", ""); | |
| value = ""; | |
| } else { | |
| Object argConverted = arg; | |
| if(arg.equals("true") || arg.equals("false")) { | |
| argConverted= Boolean.valueOf(arg); | |
| } else if(arg.matches("^\\d+$")) { | |
| argConverted= Long.valueOf(arg); | |
| } else if(arg.matches("^[0-9]+(|.\\d*[0-9])+$")) { | |
| argConverted= Double.valueOf(arg); | |
| } | |
| if (value instanceof String && ((String) value).isEmpty()) { | |
| value = argConverted; | |
| } else if (value instanceof JsonArray) { | |
| ((JsonArray) value).addString(argConverted.toString()); | |
| } else { | |
| value = new JsonArray(Arrays.asList(value, argConverted)); | |
| } | |
| } | |
| } | |
| if(value instanceof String && ((String) value).isEmpty()) { | |
| value=true; | |
| } | |
| json.put(key, value); | |
| return json; | |
| } | |
| } |
This file contains hidden or 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
| {"remove":"filename","add":["file1","file2"],"send":["fileA","fileB","fileC"],"count":1,"rate":2.343434,"quiet":false,"fast":true} |
This file contains hidden or 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
| final JsonObject jsonObject = JsonUtil.argsToJson(Arrays.asList("--remove", | |
| "filename", | |
| "-add", | |
| "file1", "file2", | |
| "-send", | |
| "fileA", "fileB", "fileC", | |
| "--count","1", | |
| "--rate","2.343434", | |
| "--quiet","false", | |
| "--fast")); | |
| System.out.println(jsonObject); | |
| assertEquals("filename", jsonObject.getString("remove")); | |
| assertEquals(true,jsonObject.getBoolean("fast")); | |
| assertEquals(false,jsonObject.getBoolean("quiet")); | |
| assertEquals(1,(long)jsonObject.getInteger("count")); | |
| assertEquals(3,(long)jsonObject.getArray("send").size()); | |
| assertEquals(2,(long)jsonObject.getArray("add").size()); | |
| assertEquals(2.343434,jsonObject.getNumber("rate").floatValue(),0.001); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment