JSON writer Introduces malformed string when serializing usIng GSON
For some reason when I try to read some metadata which has some text with double quotes, FFprobe tries to escape them from "text here" into \"text here\", which causes lots of problems on my side, as I unable to serialize the JSON properly when trying to convert to Java code. Is there a way to tell the JSON writer not to escape the double quotes inside the string of the metadata. I am using : ffprobe arrayOf( "-loglevel", "error", "-print_format", "json", "-show_error", "-show_streams", "-show_format", "-hide_banner", "-i", realPath ) -- Sincerely, Rowland Rowland
On Thu, Feb 13, 2020 at 13:22:18 +0300, Otieno Rowland wrote:
For some reason when I try to read some metadata which has some text with double quotes, FFprobe tries to escape them from "text here" into \"text here\", which causes lots of problems on my side, as I unable to serialize the JSON properly when trying to convert to Java code.
Can you give a complete example of the metadata, as shown by "ffmpeg -i" or "ffprobe" (without "-hide_banner", and without the other ffprobe options), the JSON output, and what you are expecting, please? Correct JSON *mandates* that certain characters be escaped, and double quotes inside the (keys and) values most certainly *must* be escaped! Assuming a value such as: This text has "some" quotation marks the correct JSON is: "keyname": "This text has \"some\" quotation marks" Cheers, Moritz
(Replying to myself.) On Thu, Feb 13, 2020 at 11:52:11 +0100, Moritz Barsnick wrote:
On Thu, Feb 13, 2020 at 13:22:18 +0300, Otieno Rowland wrote:
For some reason when I try to read some metadata which has some text with double quotes, FFprobe tries to escape them from "text here" into \"text here\", which causes lots of problems on my side, as I unable to serialize the JSON properly when trying to convert to Java code.
Can you give a complete example of the metadata, as shown by "ffmpeg -i" or "ffprobe" (without "-hide_banner", and without the other ffprobe options), the JSON output, and what you are expecting, please?
Sorry, I did it on my own. Assuming you mean something like this: $ ffmpeg -f lavfi -i testsrc=d=0.1 -metadata blafoo='This is some "quoted" text' -f matroska - | ffprobe -print_format json -show_format - the JSON output is "format": { "filename": "pipe:", "nb_streams": 1, "nb_programs": 0, "format_name": "matroska,webm", "format_long_name": "Matroska / WebM", "probe_score": 100, "tags": { "BLAFOO": "This is some \"quoted\" text", "ENCODER": "Lavf58.35.102" } } This is fully correct JSON. What you need is a proper JSON parser - it will understand this quoting and escaping. I'm sure there are many JSON parsers available for Java. *Every* one of them must handle this. (I only have experience with various Perl, C and C++ parsers.) Writing your own can trip over this, but shouldn't be too tricky, JSON doesn't allow for many exceptions in my experience (unlike the curiosities of JavaScript/ECMAScript). Cheers, Moritz
On 2/13/2020 3:00 AM, Moritz Barsnick wrote:
This is fully correct JSON. What you need is a proper JSON parser - it will understand this quoting and escaping.
Many people use regular expressions as a "simple" way to parse JSON (heck, I've done that); this often works, but when it doesn't it can fail spectacularly. At least in this case, use a real parser. (If the OP -is- using a non-regexp parser, try a different one.) BTW, some people recommend quoting all values, not just strings (e.g. "probe_score": "100"); I don't have an opinion on that but I can't see it hurting anything. Later, z!
On Thu, Feb 13, 2020 at 07:59:31 -0800, Carl Zwanzig wrote:
BTW, some people recommend quoting all values, not just strings (e.g. "probe_score": "100"); I don't have an opinion on that but I can't see it hurting anything.
No, assuming there's a schema or a value type definition behind those, that's not correct. An integer type value is *never* to be quoted. A boolean type value is also *never* quoted, it is incorrect if encoded as '"true"', it's always just 'true'. The parser may not be aware of a schema, but it would e.g. receive a string "true" and not know that it's meant to represent a boolean value. I had to fight some formatting errors (made by myself) recently, and the receiving side correctly complained. ffprobe's output is, to my knowledge, schema based, so the user needs to get what they expect. Sorry for off topic, cheers, Moritz
participants (3)
-
Carl Zwanzig -
Moritz Barsnick -
Otieno Rowland