-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
McJsonEscapeString (overzealous) #13
Comments
Hi, new information, I searched and found these two functions: https://www.freepascal.org/docs-html/fcl/fpjson/stringtojsonstring.html These work exactly as I want and only convert the characters you really need, leaving the json completely human readable and editable (for the translators, in my case). It doesn't even convert the "/" character, or only if you specifically ask it to, see: strict parameter. But since your function library supports several programming environments, you obviously cannot solve it with fpc functions. But this is a good example of the fact that only really necessary conversions are needed, the official freepascal function shows this. Thank you! |
I think I can improve |
Thank you! For me, it would be best if it worked exactly like the two functions from the fpjson unit.
If this is the Strict:= false; then for me its okay. Just theory (preserve the capabilities of your code): type TMCJsonEscapeLevels=(elNormal, elStrict, elForce) Normal level escape see above. |
Hi, Just checking:
I think I can do this using a new constructor as a way to pass a Also, I think I can automatically use I'll try. |
Thanks, that sounds good, just a note, these levels refer to the escape function, unescape can always be completely (force) like fpjson.JSONStringToString works, or it can be controlled, for example: type TMCJsonEscapeLevels=(elNormal, elStrict, elForce);
function McJsonEscapeString (const aStr: string; const aStrict: TMCJsonEscapeLevels = elNormal): string;
function McJsonUnEscapeString (const aStr: string; const aStrict: TMCJsonEscapeLevels = elForce): string; Naturally, if it will be automatic, then for example: type TMCJsonEscapeLevels=(elNone, elNormal, elStrict, elForce);
property
AutoEscape: TMCJsonEscapeLevels read FAutoEscape write FAutoEscape default elNormal;
AutoUnEscape: TMCJsonEscapeLevels read FAutoUnEscape write FAutoUnEscape default elForce; elNone for the speed tests... or for the custom usage (like as works now). |
Hi, Right, I need this Also, since The var
JNone, JNormal, JJson, JForce: TMcJsonItem;
begin
JNone := TMcJsonItem.Create; //elNone is the default
JNone.AsJSON := '{"path":"C:\\Windows"}'; //or use McJsonEscapeString
IsEqual := ( JNone['path'].AsString = 'C:\\Windows' );
JNormal := TMcJsonItem.Create(elNormal);
JNormal.AsJSON := '{"path":"C:\Windows"}'; //internally = '{"path":"C:\\Windows"}'
IsEqual := ( JNormal['path'].AsString = 'C:\Windows' );
JJson:= TMcJsonItem.Create(elNormal);
JJson.AsJSON := '{"json":"{"i": 123}"}'; //internally = '{"json":"{\"i\":123}"}'
IsEqual := ( JJson['json'].AsString = '{"i":123}' );
JForce:= TMcJsonItem.Create(elForce);
JForce.AsJSON := '{"force":"a#13bç"}'; //internally = '{"force":"a\nb\u00E7"}' considering CP1252
IsEqual := ( JForce['json'].AsString = 'a#13bç' );
end;
One problem about "auto escaping" is that all the chars position might change when there are insertions, so the error messages about parsing might have wrong What I don't get yet is how see escaped strings within levels Revising all this subject I've realized that IsEqual := ( McJsonUnEscapeString('\b\t\r\f\n') = #8#9#10#12#13 ); |
Thank you for your work, I read it about 3 times, but I still don't fully understand it. :) I wrote earlier that you need escape/unescape ONLY for storage (file). I understand that immediately storing the escape values in the structure can be problematic, but you see this better. As I wrote before, what if escape/unescape was activated only when saving/loading? But if you think that what you wrote can be solved completely safely, for example: "JForce:= TMcJsonItem.Create(elNormal);" with AsString getter/setter, that makes perfect sense to me. However you solve it, I think I'll be able to use it. If you release a test version, I will try it. Thank you! |
Hi, I was thinking beyond just escape/unescape when saving/loading. I'm trying to improve To sum up, I'm trying to make users' experience easier, like in the Also with: JJson:= TMcJsonItem.Create(elNormal);
JJson.AsJSON := '{"json":"{"i": 123}"}'; //internally = '{"json":"{\"i\":123}"}'
IsEqual := ( JJson['json'].AsString = '{"i":123}' ); This example will also work if you are reading an unescaped fle: JJson:= TMcJsonItem.Create(elNormal);
JJson.LoadFromFile('unescaped.json'); // UTF-8 content like {"json":"{"i": 123}"}
IsEqual := ( JJson['json'].AsString = '{"i":123}' ); But, my main concern is that an unescaped JSON file with content like I think this leads me to focus efforts only on JNormal := TMcJsonItem.Create(elNormal);
JNormal.Add('path1').AsString := 'C:\Windows'; //auto-escaped to '{"path1":"C:\\Windows"}'
// or
JNormal.S['path2'] := 'C:\Users'; //auto-escaped to '{"path2":"C:\\Users"}'
IsEqual := ( JNormal['path1'].AsString = 'C:\Windows' );
IsEqual := ( JNormal.S['path2'] = 'C:\Users' ); Could you share again an example of what file are you dealing with and how do you want to use |
Since your json library is suitable for handling any json file, I don't really understand the request, but I'll show you an example naturally. By giving suggestions, your library develops and learns more and more. In the active project, I generate a json file completely from scratch to make it transparent and modifiable. But this is only file-level data (write/read) because I basically store (and works) the data in an internal structure: type
TCustomLanguageItemList = specialize TFPGMap<integer, TLanguageItem>; The reason for this is, for example, that it is necessary to sort them in ascending order by key (I didn't see a sort option in your library). Simplified (!) examples as you ask. About 20000 keys: {
"200": {
"OrgSrc": "Value",
"ModSrc": "Value",
"OrgDst": "Value",
"ModDst": "Value",
"Info": ""}
} Src := JSONStringToString(aJsonItem.Values[ltSrc.NameStr].AsString);
Dst := JSONStringToString(aJsonItem.Values[ltDst.NameStr].AsString);
aJsonItem.Values[ltSrc.NameStr].AsString := StringToJSONString(Src);
aJsonItem.Values[ltDst.NameStr].AsString := StringToJSONString(Dst); Value := JSONStringToString(JsonItem.Values[lt.NameStr].AsString);
JsonID.Add(lt.NameStr).AsString := StringToJSONString(Self.Data[i].GetItem(lt)); |
Please, edit your previous post, focus into the JSON tiny example (related to a 20,000 keys file), in order to see the necessity of using |
As you requested. |
Hi, {
"200": {
"OrgSrc": "Value",
"ModSrc": "Value",
"OrgDst": "Value",
"ModDst": "Value",
"Info": ""}
} But it is OK. I'm finishing a version that JNormal := TMcJsonItem.Create(jetNormal);
JNormal.Add('path1').AsString := 'C:\Windows'; //auto-escaped to '{"path1":"C:\\Windows"}'
JNormal.S['path2'] := '"C:\New Users"'; //auto-escaped to '{"path2":"\"C:\\New Users\""}'
IsEqual := ( JNormal['path1'].AsString = 'C:\Windows' );
IsEqual := ( JNormal.S['path2'] = '"C:\New Users"' ); |
Hi, about 20,000 keys with texts, a lot of json forbidden characters are included, another project has many more keys and texts. I don't think I knew what you were asking exactly. If I don't use text-json conversion, the text will surely be wrong somewhere, which is why I used the json text conversion procedure. ps.: Just a note, I've been getting a message from your library when compiling for quite some time: |
Just a note again, as I know you pay attention to the speed, as I measure the time of the entire processing, it quickly became apparent that your json-text conversion functions are a little slower than those of the fpsjon unit, the total processing time (nothing changed apart from these functions) fpjson : 0.58 secs, with MCJson 0.87 secs. |
Hi, Thanks for the reply. In order to fix the warning related to About the performance, I'll take a second look at Regards, |
Thank you, its works for me: function ToString: string; {$IFNDEF FPC} overload; {$ELSE} override; {$ENDIF} // Supress fpc compiler warning... Thanks for everything! |
Hi! I was avoiding I'll revise my goals here. Thanks. |
Hi!
Very-very thanks for this feature, but it's very overzealous as it also converts all accented characters, which isn't necessarily lucky since json is a human-readable/modifiable format (It is used by translators, for example).
So, I think this code uneccesary:
But if you need this, I suggest more parameters, for example:
The text was updated successfully, but these errors were encountered: