|
11 | 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
12 | 12 | import com.fasterxml.jackson.databind.SerializationFeature; |
13 | 13 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 14 | +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; |
| 15 | +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| 16 | +import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; |
14 | 17 |
|
15 | 18 | import io.github.jeemv.springboot.vuejs.VueJS; |
16 | 19 | import io.github.jeemv.springboot.vuejs.beans.RawObject; |
|
43 | 46 | import io.github.jeemv.springboot.vuejs.serializers.WatchersSerializer; |
44 | 47 |
|
45 | 48 | /** |
46 | | - * JsUtils |
47 | | - * This class is part of springBoot-VueJS |
| 49 | + * JsUtils This class is part of springBoot-VueJS |
| 50 | + * |
48 | 51 | * @author jcheron myaddressmail@gmail.com |
49 | | - * @version 1.0.0 |
| 52 | + * @version 1.0.1 |
50 | 53 | * |
51 | 54 | */ |
52 | 55 | public class JsUtils { |
53 | | - |
| 56 | + |
54 | 57 | /** |
55 | 58 | * Returns a JSON string from an object, using defined serializers |
| 59 | + * |
56 | 60 | * @param o the object to parse |
57 | 61 | * @return the JSON string |
58 | 62 | * @throws JsonProcessingException |
59 | 63 | */ |
60 | 64 | public static String objectToJSON(Object o) throws JsonProcessingException { |
61 | | - ObjectMapper objectMapper = new ObjectMapper(); |
62 | | - objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
63 | | - SimpleModule module = new SimpleModule(); |
64 | | - module.addSerializer(VueJS.class, new VueJSSerializer()); |
65 | | - module.addSerializer(VueComponent.class, new VueComponentSerializer()); |
66 | | - module.addSerializer(VueMethods.class, new MethodsSerializer()); |
67 | | - module.addSerializer(VueMethod.class, new MethodSerializer()); |
68 | | - module.addSerializer(VueComputeds.class, new ComputedsSerializer()); |
69 | | - module.addSerializer(VueComputed.class, new ComputedSerializer()); |
70 | | - module.addSerializer(VueWatchers.class, new WatchersSerializer()); |
71 | | - module.addSerializer(VueWatcher.class, new WatcherSerializer()); |
72 | | - module.addSerializer(VueProp.class, new PropSerializer()); |
73 | | - module.addSerializer(VueProps.class, new PropsSerializer()); |
74 | | - module.addSerializer(AbstractVueComposition.class, new AbstractCompositionSerializer()); |
75 | | - module.addSerializer(VueDirectives.class, new DirectivesSerializer()); |
76 | | - module.addSerializer(VueFilters.class, new FiltersSerializer()); |
77 | | - module.addSerializer(RawObject.class, new RawObjectSerializer()); |
78 | | - objectMapper.registerModule(module); |
79 | | - if(VueConfig.debug) { |
80 | | - objectMapper.enable(SerializationFeature.INDENT_OUTPUT); |
81 | | - } |
82 | | - return objectMapper.writeValueAsString(o); |
| 65 | + ObjectMapper objectMapper = new ObjectMapper().registerModule(new ParameterNamesModule()) |
| 66 | + .registerModule(new Jdk8Module()).registerModule(new JavaTimeModule()); |
| 67 | + objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
| 68 | + SimpleModule module = new SimpleModule(); |
| 69 | + module.addSerializer(VueJS.class, new VueJSSerializer()); |
| 70 | + module.addSerializer(VueComponent.class, new VueComponentSerializer()); |
| 71 | + module.addSerializer(VueMethods.class, new MethodsSerializer()); |
| 72 | + module.addSerializer(VueMethod.class, new MethodSerializer()); |
| 73 | + module.addSerializer(VueComputeds.class, new ComputedsSerializer()); |
| 74 | + module.addSerializer(VueComputed.class, new ComputedSerializer()); |
| 75 | + module.addSerializer(VueWatchers.class, new WatchersSerializer()); |
| 76 | + module.addSerializer(VueWatcher.class, new WatcherSerializer()); |
| 77 | + module.addSerializer(VueProp.class, new PropSerializer()); |
| 78 | + module.addSerializer(VueProps.class, new PropsSerializer()); |
| 79 | + module.addSerializer(AbstractVueComposition.class, new AbstractCompositionSerializer()); |
| 80 | + module.addSerializer(VueDirectives.class, new DirectivesSerializer()); |
| 81 | + module.addSerializer(VueFilters.class, new FiltersSerializer()); |
| 82 | + module.addSerializer(RawObject.class, new RawObjectSerializer()); |
| 83 | + objectMapper.registerModule(module); |
| 84 | + if (VueConfig.debug) { |
| 85 | + objectMapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 86 | + } |
| 87 | + return objectMapper.writeValueAsString(o); |
83 | 88 | } |
84 | | - |
85 | | - public static <T> T jsonStringToObject(String jsonString,Class<T> clazz) throws JsonParseException, JsonMappingException, IOException { |
| 89 | + |
| 90 | + public static <T> T jsonStringToObject(String jsonString, Class<T> clazz) |
| 91 | + throws JsonParseException, JsonMappingException, IOException { |
86 | 92 | ObjectMapper mapper = new ObjectMapper(); |
87 | | - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
88 | | - T o = mapper.readValue(jsonString,clazz); |
| 93 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 94 | + T o = mapper.readValue(jsonString, clazz); |
89 | 95 | return o; |
90 | 96 | } |
91 | | - |
| 97 | + |
92 | 98 | public static String cleanJS(String javascriptCode) { |
93 | | - String result=javascriptCode; |
94 | | - result=indent(splitLines(result),"\t"); |
| 99 | + String result = javascriptCode; |
| 100 | + result = indent(splitLines(result), "\t"); |
95 | 101 | return result; |
96 | 102 | } |
97 | | - |
| 103 | + |
98 | 104 | private static String[] splitLines(String javascriptCode) { |
99 | | - javascriptCode=Matcher.quoteReplacement(javascriptCode); |
| 105 | + javascriptCode = Matcher.quoteReplacement(javascriptCode); |
100 | 106 | Pattern regex = Pattern.compile("\\'[^']*'|(?:\\\"(?:\\\\\\\"|[^\\\"])*\\\")|([;\\\\{\\\\}])"); |
101 | 107 | Matcher m = regex.matcher(javascriptCode); |
102 | | - StringBuffer b= new StringBuffer(); |
| 108 | + StringBuffer b = new StringBuffer(); |
103 | 109 | while (m.find()) { |
104 | | - if(m.group(1) != null) { |
105 | | - if("}".equals(m.group(1))) { |
106 | | - m.appendReplacement(b, "SplitHere"+m.group(0)+"SplitHere"); |
107 | | - }else { |
108 | | - m.appendReplacement(b, m.group(0)+"SplitHere"); |
109 | | - } |
110 | | - } |
111 | | - else m.appendReplacement(b, m.group(0).replace("\"", "\\\"")); |
| 110 | + if (m.group(1) != null) { |
| 111 | + if ("}".equals(m.group(1))) { |
| 112 | + m.appendReplacement(b, "SplitHere" + m.group(0) + "SplitHere"); |
| 113 | + } else { |
| 114 | + m.appendReplacement(b, m.group(0) + "SplitHere"); |
| 115 | + } |
| 116 | + } else |
| 117 | + m.appendReplacement(b, m.group(0).replace("\"", "\\\"")); |
112 | 118 | } |
113 | 119 | m.appendTail(b); |
114 | 120 | String replaced = b.toString(); |
115 | 121 | return replaced.split("SplitHere"); |
116 | 122 | } |
117 | | - |
118 | | - private static String indent(String[] javascriptLines,String tabulation) { |
119 | | - StringBuffer sb=new StringBuffer(); |
120 | | - int dec=0; |
121 | | - for(String line:javascriptLines) { |
122 | | - if(!"".equals(line.trim())) { |
123 | | - if(!line.startsWith("\r\n") && sb.length()>0) { |
| 123 | + |
| 124 | + private static String indent(String[] javascriptLines, String tabulation) { |
| 125 | + StringBuffer sb = new StringBuffer(); |
| 126 | + int dec = 0; |
| 127 | + for (String line : javascriptLines) { |
| 128 | + if (!"".equals(line.trim())) { |
| 129 | + if (!line.startsWith("\r\n") && sb.length() > 0) { |
124 | 130 | sb.append("\r\n"); |
125 | 131 | } |
126 | | - if(dec>0) { |
| 132 | + if (dec > 0) { |
127 | 133 | sb.append(new String(new char[dec]).replace("\0", tabulation)); |
128 | 134 | } |
129 | | - if(line.endsWith("{")) { |
| 135 | + if (line.endsWith("{")) { |
130 | 136 | dec++; |
131 | 137 | } |
132 | | - if(line.startsWith("}")) { |
| 138 | + if (line.startsWith("}")) { |
133 | 139 | dec--; |
134 | 140 | } |
135 | 141 | sb.append(line); |
136 | 142 | } |
137 | 143 | } |
138 | 144 | return sb.toString(); |
139 | 145 | } |
140 | | - |
| 146 | + |
141 | 147 | public static String wrapScript(String script) { |
142 | | - if(script==null || "".equals(script)) { |
| 148 | + if (script == null || "".equals(script)) { |
143 | 149 | return ""; |
144 | 150 | } |
145 | | - if(!script.startsWith("<script>")) { |
146 | | - script="<script>"+script+"</script>"; |
| 151 | + if (!script.startsWith("<script>")) { |
| 152 | + script = "<script>" + script + "</script>"; |
147 | 153 | } |
148 | 154 | return script; |
149 | 155 | } |
|
0 commit comments