Last active
July 25, 2022 04:16
-
-
Save sutra/da7cd596014cf555f1105e492192a15b to your computer and use it in GitHub Desktop.
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
String makeToJsonStr(String str) { | |
var keys = "label|fieldType|searchType|editHide|addHide|hide|disabled|required|comment|options"; | |
var regex = "(?<key>" + keys + ")\\s*:(?<value>(?:(?!(" + keys + "):).)*)(?<delimiter>[\\,\\}])"; | |
var replacement = "\"${key}\":\"${value}\"${delimiter}"; | |
var replaced = str.replaceAll(regex, replacement); | |
return replaced; | |
} | |
@Test | |
void testMakeToJsonStr() { | |
var str = "{label :用户 id,searchType:1,hide:1,disabled:1,required:1,options:1:yes:tag-info,2:no:tag-danger}"; | |
var replaced = makeToJsonStr(str); | |
var expected = """ | |
{"label":"用户 id","searchType":"1","hide":"1","disabled":"1","required":"1","options":"1:yes:tag-info,2:no:tag-danger"}"""; | |
assertEquals(expected, replaced); | |
} | |
@Test | |
void testMakeToJsonStr_keyInValueButDoesNotHaveColon_keyAfterOptionsValueAndHasColon() { | |
var str = "{label :用户 id,searchType:1,hide:1,disabled:1,required:1,options:1:yes:tag-info,2:no,fieldType,fieldType:tag-danger}"; | |
var replaced = makeToJsonStr(str); | |
var expected = """ | |
{"label":"用户 id","searchType":"1","hide":"1","disabled":"1","required":"1","options":"1:yes:tag-info,2:no,fieldType","fieldType":"tag-danger"}"""; | |
assertEquals(expected, replaced); | |
} |
Author
sutra
commented
Jul 24, 2022
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment