Skip to content

Commit 184a183

Browse files
author
Clement Champetier
committed
properties: fix json validity on Windows
* Added JsonStreamWriter::escapeJsonString to escape strings accordingly to the JSON standard.
1 parent 5bbb898 commit 184a183

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/AvTranscoder/properties/JsonWriter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
#include "JsonWriter.hpp"
22

3+
#include <string>
4+
#include <sstream>
5+
36
namespace avtranscoder {
47
namespace json {
58

9+
std::string JsonStreamWriter::escapeJsonString(const std::string& input)
10+
{
11+
std::ostringstream ss;
12+
for (std::string::const_iterator iter = input.begin(); iter != input.end(); iter++) {
13+
switch (*iter) {
14+
case '\\': ss << "\\\\"; break;
15+
default: ss << *iter; break;
16+
}
17+
}
18+
return ss.str();
19+
}
20+
621
template <>
722
JsonStreamWriter& JsonStreamWriter::operator<<(bool value)
823
{

src/AvTranscoder/properties/JsonWriter.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class AvExport JsonStreamWriter
5252
}
5353

5454
virtual std::ostream& finish() = 0;
55+
56+
// Escape strings accordingly to the JSON standard
57+
std::string escapeJsonString(const std::string& input);
5558
};
5659

5760
// Write a boolean to the stream.
@@ -76,6 +79,15 @@ class AvExport JsonObjectStreamWriter : public JsonStreamWriter
7679
return *this;
7780
}
7881

82+
template<>
83+
JsonObjectStreamWriter& operator<<(const std::pair<const char *, const char*> pair)
84+
{
85+
std::string first(pair.first);
86+
std::string second(pair.second);
87+
addSep() << escapeJsonString(first).c_str() << ':' << escapeJsonString(second).c_str();
88+
return *this;
89+
}
90+
7991
protected:
8092
virtual std::ostream& finish() { return stream << '}'; }
8193
};

test/pyTest/testProperties.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,5 @@ def testCheckFilePropertiesAsJson():
138138
inputFile = av.InputFile( inputFileName )
139139

140140
import json
141-
# throw exception if it is not a valid JSON
141+
# json.loads method throws a ValueError if it is not a valid JSON.
142142
json.loads(inputFile.getProperties().allPropertiesAsJson())
143-
144-

0 commit comments

Comments
 (0)