<div dir="ltr">Thanks, I will try this escape mechanism for the returned C string.</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Mar 28, 2016 at 1:04 PM, Greg Clayton <span dir="ltr"><<a href="mailto:gclayton@apple.com" target="_blank">gclayton@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Mar 28, 2016, at 11:38 AM, Jeffrey Tan <<a href="mailto:jeffrey.fudan@gmail.com">jeffrey.fudan@gmail.com</a>> wrote:<br>
><br>
> Thanks Greg for the detailed explanation, very helpful.<br>
> 1. Just to confirm, the weird string displayed is because 'data_' points to some random memory?<br>
<br>
</span>Yes.<br>
<span class=""><br>
> So what gdb displays is also some random memory content not something that more meaningful than us? I thought we(lldb) did not display std::string content well but gdb does it correct.<br>
<br>
</span>So the "size_" variable is zero, so anything that GDB is displaying is shear luck of what the contents of memory are that "data_" points to. You can't rely on any contents of "data_" since it is clearly bogus. What you really want to see is just the string that "std::string" points to:<br>
<br>
(std::string) my_string = "Hello"<br>
<br>
Or for a std::string that contains 0, 1, and 2 as characters:<br>
<br>
(std::string) my_string = "\x00\x01\x02"<br>
<span class=""><br>
<br>
<br>
> 2. I guess the std::string formatter did not kick in because our company may link some special stl implementation. Let me share our binary for you to confirm.<br>
<br>
</span>You can get some help from Enrico to see why things are not displaying correctly. My guess is this C++ standard library is different from the ones that we added support for.<br>
<span class=""><br>
> 3. I dumped the content of the object we try to json.dumps() against, here is the content:<br>
> response: {'id': 57, 'result': {'result': [{'name': 'data_', 'value': {'type': 'object', 'description': '(char *) "\xc9\xc3UH\\x89\xe5H\x8 9}\xf8H\\x8bE\xf8]\xc3\x90UH\\x89\xe5H\x83\xec\x10H\\x89}\xf8H\\x8bE\xf8H\\x89\xc7\xe8~\\xb4\xca\xff\\x90\xc9\xc3UH\\x89\ xe5SH\\x83\xec\x18H\\x89}\xe8H\x89u\xe0H\x8bE\xe0H\x89\xc7\xe8\\x9e\xff\xff\xffH\\x8b\\x18H\\x8bE\xe8H\x89\xc7\xe8O\\xb4\ xca\xffH\\x89\xc6\xbf\\b"', 'objectId': 'RemoteObjectManager.118'}}, {'name': 'size_', 'value': {'type': 'object', 'descr iption': '(std::size_t) 0'}}, {'name': 'capacity_', 'value': {'type': 'object', 'description': '(std::size_t) 14411518807 58558720'}}]}}<br>
> So seems that the problem is json.dumps() is trying to treat the raw byte array as utf8 which failed.<br>
> So we need to figure out how to escape the raw byte array into string so that we can json.dumps() it. The key question is how do we know the correct encoding of the byte array.<br>
<br>
</span>It doesn't really matter. Just know that any of the strings from:<br>
<br>
const char *SBValue::GetName();<br>
const char *SBValue::GetTypeName ();<br>
const char *SBValue::GetDisplayTypeName();<br>
const char *SBValue::GetValue();<br>
const char *SBValue::GetSummary();<br>
const char *SBValue::GetObjectDescription();<br>
const char *SBValue::GetLocation ();<br>
<br>
Will need to be escaped.<br>
<span class=""><br>
> Is my understanding correct that only the formatter has the knowledge to decode the byte array correctly?<br>
<br>
</span>We dump the values as strings. You won't get bytes out. You might get UTF8 bytes or other things that JSON might interpret as special characters and any C strings that you get from the above calls will just need to be escaped if needed.<br>
<span class=""><br>
> If we fail to find a type formatter(which is this case) and get a raw field with byte array, we have no knowledge of the encoding so either we have to guess one default encoding and try it or just display the raw byte array content instead of decoding it?<br>
<br>
</span>Again, this is all C strings. I don't think anything else matters.<br>
<br>
Our JSON.cpp has the following:<br>
<br>
int<br>
JSONParser::GetEscapedChar(bool &was_escaped)<br>
{<br>
was_escaped = false;<br>
const char ch = GetChar();<br>
if (ch == '\\')<br>
{<br>
was_escaped = true;<br>
const char ch2 = GetChar();<br>
switch (ch2)<br>
{<br>
case '"':<br>
case '\\':<br>
case '/':<br>
default:<br>
break;<br>
<br>
case 'b': return '\b';<br>
case 'f': return '\f';<br>
case 'n': return '\n';<br>
case 'r': return '\r';<br>
case 't': return '\t';<br>
case 'u':<br>
{<br>
const int hi_byte = DecodeHexU8();<br>
const int lo_byte = DecodeHexU8();<br>
if (hi_byte >=0 && lo_byte >= 0)<br>
return hi_byte << 8 | lo_byte;<br>
return -1;<br>
}<br>
break;<br>
}<br>
return ch2;<br>
}<br>
return ch;<br>
}<br>
<br>
You can see how it is used when the JSON parser is parsing in JSONParser::GetToken() in the '"' case.</blockquote></div><br></div>