Serializer skips null values

This commit is contained in:
Johan Bowald 2018-03-28 14:44:45 +02:00
parent ae751c4882
commit faa2722f45

View File

@ -3298,9 +3298,10 @@ static void SerializeNumberArrayProperty(const std::string &key,
for (unsigned int i = 0; i < value.size(); ++i) { for (unsigned int i = 0; i < value.size(); ++i) {
vals.push_back(static_cast<double>(value[i])); vals.push_back(static_cast<double>(value[i]));
} }
if (!vals.is_null()) {
obj[key] = vals; obj[key] = vals;
} }
}
static void SerializeStringProperty(const std::string &key, static void SerializeStringProperty(const std::string &key,
const std::string &value, json &obj) { const std::string &value, json &obj) {
@ -3477,10 +3478,20 @@ static void SerializeGltfBuffer(Buffer &buffer, json &o,
static void SerializeGltfBufferView(BufferView &bufferView, json &o) { static void SerializeGltfBufferView(BufferView &bufferView, json &o) {
SerializeNumberProperty("buffer", bufferView.buffer, o); SerializeNumberProperty("buffer", bufferView.buffer, o);
SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o); SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o);
SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o);
SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o);
SerializeNumberProperty("target", bufferView.target, o);
// byteStride is optional, minimum allowed is 4
if (bufferView.byteStride >= 4) {
SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o);
}
// byteOffset is optional, default is 0
if (bufferView.byteOffset > 0) {
SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o);
}
// Target is optional, check if it contains a valid value
if (bufferView.target == TINYGLTF_TARGET_ARRAY_BUFFER ||
bufferView.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER) {
SerializeNumberProperty("target", bufferView.target, o);
}
if (bufferView.name.size()) { if (bufferView.name.size()) {
SerializeStringProperty("name", bufferView.name, o); SerializeStringProperty("name", bufferView.name, o);
} }
@ -3532,8 +3543,15 @@ static void SerializeGltfMesh(Mesh &mesh, json &o) {
} }
primitive["attributes"] = attributes; primitive["attributes"] = attributes;
// Indicies is optional
if (gltfPrimitive.indices > -1) {
SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive); SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive);
}
// Material is optional
if (gltfPrimitive.material > -1) {
SerializeNumberProperty<int>("material", gltfPrimitive.material, primitive); SerializeNumberProperty<int>("material", gltfPrimitive.material, primitive);
}
SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive); SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive);
// Morph targets // Morph targets
@ -3773,6 +3791,7 @@ bool TinyGLTF::WriteGltfSceneToFile(
} }
// IMAGES // IMAGES
if (model->images.size()) {
json images; json images;
for (unsigned int i = 0; i < model->images.size(); ++i) { for (unsigned int i = 0; i < model->images.size(); ++i) {
json image; json image;
@ -3780,8 +3799,10 @@ bool TinyGLTF::WriteGltfSceneToFile(
images.push_back(image); images.push_back(image);
} }
output["images"] = images; output["images"] = images;
}
// MATERIALS // MATERIALS
if (model->materials.size()) {
json materials; json materials;
for (unsigned int i = 0; i < model->materials.size(); ++i) { for (unsigned int i = 0; i < model->materials.size(); ++i) {
json material; json material;
@ -3789,8 +3810,10 @@ bool TinyGLTF::WriteGltfSceneToFile(
materials.push_back(material); materials.push_back(material);
} }
output["materials"] = materials; output["materials"] = materials;
}
// MESHES // MESHES
if (model->meshes.size()) {
json meshes; json meshes;
for (unsigned int i = 0; i < model->meshes.size(); ++i) { for (unsigned int i = 0; i < model->meshes.size(); ++i) {
json mesh; json mesh;
@ -3798,8 +3821,10 @@ bool TinyGLTF::WriteGltfSceneToFile(
meshes.push_back(mesh); meshes.push_back(mesh);
} }
output["meshes"] = meshes; output["meshes"] = meshes;
}
// NODES // NODES
if (model->nodes.size()) {
json nodes; json nodes;
for (unsigned int i = 0; i < model->nodes.size(); ++i) { for (unsigned int i = 0; i < model->nodes.size(); ++i) {
json node; json node;
@ -3807,11 +3832,15 @@ bool TinyGLTF::WriteGltfSceneToFile(
nodes.push_back(node); nodes.push_back(node);
} }
output["nodes"] = nodes; output["nodes"] = nodes;
}
// SCENE // SCENE
if (model->defaultScene > -1) {
SerializeNumberProperty<int>("scene", model->defaultScene, output); SerializeNumberProperty<int>("scene", model->defaultScene, output);
}
// SCENES // SCENES
if (model->scenes.size()) {
json scenes; json scenes;
for (unsigned int i = 0; i < model->scenes.size(); ++i) { for (unsigned int i = 0; i < model->scenes.size(); ++i) {
json currentScene; json currentScene;
@ -3819,6 +3848,7 @@ bool TinyGLTF::WriteGltfSceneToFile(
scenes.push_back(currentScene); scenes.push_back(currentScene);
} }
output["scenes"] = scenes; output["scenes"] = scenes;
}
// SKINS // SKINS
if (model->skins.size()) { if (model->skins.size()) {
@ -3832,6 +3862,7 @@ bool TinyGLTF::WriteGltfSceneToFile(
} }
// TEXTURES // TEXTURES
if (model->textures.size()) {
json textures; json textures;
for (unsigned int i = 0; i < model->textures.size(); ++i) { for (unsigned int i = 0; i < model->textures.size(); ++i) {
json texture; json texture;
@ -3839,8 +3870,10 @@ bool TinyGLTF::WriteGltfSceneToFile(
textures.push_back(texture); textures.push_back(texture);
} }
output["textures"] = textures; output["textures"] = textures;
}
// SAMPLERS // SAMPLERS
if (model->samplers.size()) {
json samplers; json samplers;
for (unsigned int i = 0; i < model->samplers.size(); ++i) { for (unsigned int i = 0; i < model->samplers.size(); ++i) {
json sampler; json sampler;
@ -3848,8 +3881,10 @@ bool TinyGLTF::WriteGltfSceneToFile(
samplers.push_back(sampler); samplers.push_back(sampler);
} }
output["samplers"] = samplers; output["samplers"] = samplers;
}
// CAMERAS // CAMERAS
if (model->cameras.size()) {
json cameras; json cameras;
for (unsigned int i = 0; i < model->cameras.size(); ++i) { for (unsigned int i = 0; i < model->cameras.size(); ++i) {
json camera; json camera;
@ -3857,8 +3892,10 @@ bool TinyGLTF::WriteGltfSceneToFile(
cameras.push_back(camera); cameras.push_back(camera);
} }
output["cameras"] = cameras; output["cameras"] = cameras;
}
// LIGHTS // LIGHTS
if (model->lights.size()) {
json lights; json lights;
for (unsigned int i = 0; i < model->lights.size(); ++i) { for (unsigned int i = 0; i < model->lights.size(); ++i) {
json light; json light;
@ -3866,6 +3903,7 @@ bool TinyGLTF::WriteGltfSceneToFile(
lights.push_back(light); lights.push_back(light);
} }
output["lights"] = lights; output["lights"] = lights;
}
WriteGltfFile(filename, output.dump()); WriteGltfFile(filename, output.dump());
return true; return true;