Merge pull request #4053 from kovacsv/compressed_json

Add export property for assimp json exporter to write compressed json
pull/4057/head
Kim Kulling 2021-08-31 08:51:23 +02:00 committed by GitHub
commit f888910a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 16 deletions

View File

@ -41,12 +41,17 @@ public:
enum { enum {
Flag_DoNotIndent = 0x1, Flag_DoNotIndent = 0x1,
Flag_WriteSpecialFloats = 0x2, Flag_WriteSpecialFloats = 0x2,
Flag_SkipWhitespaces = 0x4
}; };
JSONWriter(Assimp::IOStream &out, unsigned int flags = 0u) : JSONWriter(Assimp::IOStream &out, unsigned int flags = 0u) :
out(out), first(), flags(flags) { out(out), indent (""), newline("\n"), space(" "), buff (), first(false), flags(flags) {
// make sure that all formatting happens using the standard, C locale and not the user's current locale // make sure that all formatting happens using the standard, C locale and not the user's current locale
buff.imbue(std::locale("C")); buff.imbue(std::locale("C"));
if (flags & Flag_SkipWhitespaces) {
newline = "";
space = "";
}
} }
~JSONWriter() { ~JSONWriter() {
@ -70,7 +75,7 @@ public:
void Key(const std::string &name) { void Key(const std::string &name) {
AddIndentation(); AddIndentation();
Delimit(); Delimit();
buff << '\"' + name + "\": "; buff << '\"' + name + "\":" << space;
} }
template <typename Literal> template <typename Literal>
@ -78,12 +83,12 @@ public:
AddIndentation(); AddIndentation();
Delimit(); Delimit();
LiteralToString(buff, name) << '\n'; LiteralToString(buff, name) << newline;
} }
template <typename Literal> template <typename Literal>
void SimpleValue(const Literal &s) { void SimpleValue(const Literal &s) {
LiteralToString(buff, s) << '\n'; LiteralToString(buff, s) << newline;
} }
void SimpleValue(const void *buffer, size_t len) { void SimpleValue(const void *buffer, size_t len) {
@ -102,7 +107,7 @@ public:
} }
} }
buff << '\"' << cur_out << "\"\n"; buff << '\"' << cur_out << "\"" << newline;
delete[] cur_out; delete[] cur_out;
} }
@ -115,7 +120,7 @@ public:
} }
} }
first = true; first = true;
buff << "{\n"; buff << "{" << newline;
PushIndent(); PushIndent();
} }
@ -123,7 +128,7 @@ public:
PopIndent(); PopIndent();
AddIndentation(); AddIndentation();
first = false; first = false;
buff << "}\n"; buff << "}" << newline;
} }
void StartArray(bool is_element = false) { void StartArray(bool is_element = false) {
@ -135,19 +140,19 @@ public:
} }
} }
first = true; first = true;
buff << "[\n"; buff << "[" << newline;
PushIndent(); PushIndent();
} }
void EndArray() { void EndArray() {
PopIndent(); PopIndent();
AddIndentation(); AddIndentation();
buff << "]\n"; buff << "]" << newline;
first = false; first = false;
} }
void AddIndentation() { void AddIndentation() {
if (!(flags & Flag_DoNotIndent)) { if (!(flags & Flag_DoNotIndent) && !(flags & Flag_SkipWhitespaces)) {
buff << indent; buff << indent;
} }
} }
@ -156,7 +161,7 @@ public:
if (!first) { if (!first) {
buff << ','; buff << ',';
} else { } else {
buff << ' '; buff << space;
first = false; first = false;
} }
} }
@ -227,7 +232,9 @@ private:
private: private:
Assimp::IOStream &out; Assimp::IOStream &out;
std::string indent, newline; std::string indent;
std::string newline;
std::string space;
std::stringstream buff; std::stringstream buff;
bool first; bool first;
@ -765,7 +772,7 @@ void Write(JSONWriter &out, const aiScene &ai) {
out.EndObj(); out.EndObj();
} }
void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *scene, const Assimp::ExportProperties *) { void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *scene, const Assimp::ExportProperties *pProperties) {
std::unique_ptr<Assimp::IOStream> str(io->Open(file, "wt")); std::unique_ptr<Assimp::IOStream> str(io->Open(file, "wt"));
if (!str) { if (!str) {
throw DeadlyExportError("could not open output file"); throw DeadlyExportError("could not open output file");
@ -782,7 +789,12 @@ void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *sc
splitter.Execute(scenecopy_tmp); splitter.Execute(scenecopy_tmp);
// XXX Flag_WriteSpecialFloats is turned on by default, right now we don't have a configuration interface for exporters // XXX Flag_WriteSpecialFloats is turned on by default, right now we don't have a configuration interface for exporters
JSONWriter s(*str, JSONWriter::Flag_WriteSpecialFloats);
unsigned int flags = JSONWriter::Flag_WriteSpecialFloats;
if (pProperties->GetPropertyBool("JSON_SKIP_WHITESPACES", false)) {
flags |= JSONWriter::Flag_SkipWhitespaces;
}
JSONWriter s(*str, flags);
Write(s, *scenecopy_tmp); Write(s, *scenecopy_tmp);
} catch (...) { } catch (...) {

View File

@ -58,7 +58,18 @@ public:
Exporter exporter; Exporter exporter;
aiReturn res = exporter.Export(scene, "assjson", "./spider_test.json"); aiReturn res = exporter.Export(scene, "assjson", "./spider_test.json");
return aiReturn_SUCCESS == res; if (aiReturn_SUCCESS != res) {
return false;
}
Assimp::ExportProperties exportProperties;
exportProperties.SetPropertyBool("JSON_SKIP_WHITESPACES", true);
aiReturn resNoWhitespace = exporter.Export(scene, "assjson", "./spider_test_nowhitespace.json", 0u, &exportProperties);
if (aiReturn_SUCCESS != resNoWhitespace) {
return false;
}
return true;
} }
}; };