Working on tools to convert bitstreams to/from json.

This commit is contained in:
Andrzej Kapolka 2014-06-17 12:33:31 -07:00
parent 37c977af02
commit 7931173671
10 changed files with 201 additions and 5 deletions

View file

@ -49,4 +49,5 @@ add_subdirectory(assignment-client)
add_subdirectory(domain-server)
add_subdirectory(interface)
add_subdirectory(tests)
add_subdirectory(tools)
add_subdirectory(voxel-edit)

View file

@ -9,13 +9,9 @@
#
macro(AUTO_MTC TARGET ROOT_DIR)
if (NOT TARGET mtc)
add_subdirectory("${ROOT_DIR}/tools/mtc" "${ROOT_DIR}/tools/mtc")
endif ()
set(AUTOMTC_SRC ${TARGET}_automtc.cpp)
file(GLOB INCLUDE_FILES src/*.h)
add_custom_command(OUTPUT ${AUTOMTC_SRC} COMMAND mtc -o ${AUTOMTC_SRC} ${INCLUDE_FILES} DEPENDS mtc ${INCLUDE_FILES})
endmacro()
endmacro()

View file

@ -33,6 +33,7 @@ REGISTER_SIMPLE_TYPE_STREAMER(QByteArray)
REGISTER_SIMPLE_TYPE_STREAMER(QColor)
REGISTER_SIMPLE_TYPE_STREAMER(QScriptValue)
REGISTER_SIMPLE_TYPE_STREAMER(QString)
REGISTER_SIMPLE_TYPE_STREAMER(QVariant)
REGISTER_SIMPLE_TYPE_STREAMER(QUrl)
REGISTER_SIMPLE_TYPE_STREAMER(QVariantList)
REGISTER_SIMPLE_TYPE_STREAMER(QVariantHash)

View file

@ -805,6 +805,8 @@ public:
template<class T> JSONWriter& operator<<(const T& value) { _contents.append(getData(value)); return *this; }
void appendToContents(const QJsonValue& value) { _contents.append(value); }
void addSharedObject(const SharedObjectPointer& object);
void addObjectStreamer(const ObjectStreamer* streamer);
void addTypeStreamer(const TypeStreamer* streamer);
@ -893,6 +895,8 @@ public:
template<class T> JSONReader& operator>>(T& value) { putData(*_contentsIterator++, value); return *this; }
QJsonValue retrieveNextFromContents() { return *_contentsIterator++; }
TypeStreamerPointer getTypeStreamer(const QString& name) const;
ObjectStreamerPointer getObjectStreamer(const QString& name) const { return _objectStreamers.value(name); }
SharedObjectPointer getSharedObject(int id) const { return _sharedObjects.value(id); }

View file

@ -255,6 +255,8 @@ static bool testSerialization(Bitstream::MetadataType metadataType) {
jsonWriter << endRead;
QByteArray encodedJson = jsonWriter.getDocument().toJson();
qDebug() << encodedJson;
// and read from JSON
JSONReader jsonReader(QJsonDocument::fromJson(encodedJson), Bitstream::ALL_GENERICS);
jsonReader >> testObjectReadA;

6
tools/CMakeLists.txt Normal file
View file

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 2.8)
# add the tool directories
add_subdirectory(bitstream2json)
add_subdirectory(json2bitstream)
add_subdirectory(mtc)

View file

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 2.8)
if (WIN32)
cmake_policy (SET CMP0020 NEW)
endif (WIN32)
set(TARGET_NAME bitstream2json)
set(ROOT_DIR ../..)
set(MACRO_DIR "${ROOT_DIR}/cmake/macros")
find_package(Qt5 COMPONENTS Network Script Widgets)
include(${MACRO_DIR}/SetupHifiProject.cmake)
setup_hifi_project(${TARGET_NAME} TRUE)
link_hifi_library(metavoxels ${TARGET_NAME} "${ROOT_DIR}")
link_hifi_library(shared ${TARGET_NAME} "${ROOT_DIR}")
target_link_libraries(${TARGET_NAME} Qt5::Network Qt5::Widgets Qt5::Script)

View file

@ -0,0 +1,70 @@
//
// main.cpp
// tools/bitstream2json/src
//
// Created by Andrzej Kapolka on 6/17/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
#include <iostream>
#include <QCoreApplication>
#include <QDataStream>
#include <QFile>
#include <AttributeRegistry.h>
using namespace std;
int main (int argc, char** argv) {
// need the core application for the script engine
QCoreApplication app(argc, argv);
if (argc < 3) {
cerr << "Usage: bitstream2json inputfile outputfile [types...]" << endl;
return 0;
}
QFile inputFile(argv[1]);
if (!inputFile.open(QIODevice::ReadOnly)) {
cerr << "Failed to open input file: " << inputFile.errorString().toLatin1().constData() << endl;
return 1;
}
QDataStream inputData(&inputFile);
Bitstream input(inputData, Bitstream::FULL_METADATA, Bitstream::ALL_GENERICS);
QFile outputFile(argv[2]);
if (!outputFile.open(QIODevice::WriteOnly)) {
cerr << "Failed to open output file: " << outputFile.errorString().toLatin1().constData() << endl;
return 1;
}
JSONWriter output;
if (argc < 4) {
// default type is a single QVariant
QVariant value;
input >> value;
output << value;
} else {
for (int i = 3; i < argc; i++) {
int type = QMetaType::type(argv[i]);
if (type == QMetaType::UnknownType) {
cerr << "Unknown type: " << argv[i] << endl;
return 1;
}
const TypeStreamer* streamer = Bitstream::getTypeStreamer(type);
if (!streamer) {
cerr << "Non-streamable type: " << argv[i] << endl;
return 1;
}
QVariant value = streamer->read(input);
output.appendToContents(streamer->getJSONData(output, value));
}
}
outputFile.write(output.getDocument().toJson());
return 0;
}

View file

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 2.8)
if (WIN32)
cmake_policy (SET CMP0020 NEW)
endif (WIN32)
set(TARGET_NAME json2bitstream)
set(ROOT_DIR ../..)
set(MACRO_DIR "${ROOT_DIR}/cmake/macros")
find_package(Qt5 COMPONENTS Network Script Widgets)
include(${MACRO_DIR}/SetupHifiProject.cmake)
setup_hifi_project(${TARGET_NAME} TRUE)
link_hifi_library(metavoxels ${TARGET_NAME} "${ROOT_DIR}")
link_hifi_library(shared ${TARGET_NAME} "${ROOT_DIR}")
target_link_libraries(${TARGET_NAME} Qt5::Network Qt5::Widgets Qt5::Script)

View file

@ -0,0 +1,76 @@
//
// main.cpp
// tools/json2bitstream/src
//
// Created by Andrzej Kapolka on 6/17/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
#include <iostream>
#include <QCoreApplication>
#include <QDataStream>
#include <QFile>
#include <AttributeRegistry.h>
using namespace std;
int main (int argc, char** argv) {
// need the core application for the script engine
QCoreApplication app(argc, argv);
if (argc < 3) {
cerr << "Usage: bitstream2json inputfile outputfile [types...]" << endl;
return 0;
}
QFile inputFile(argv[1]);
if (!inputFile.open(QIODevice::ReadOnly)) {
cerr << "Failed to open input file: " << inputFile.errorString().toLatin1().constData() << endl;
return 1;
}
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(inputFile.readAll(), &error);
if (error.error != QJsonParseError::NoError) {
cerr << "Failed to read input file: " << error.errorString().toLatin1().constData() << endl;
return 1;
}
JSONReader input(document, Bitstream::ALL_GENERICS);
QFile outputFile(argv[2]);
if (!outputFile.open(QIODevice::WriteOnly)) {
cerr << "Failed to open output file: " << outputFile.errorString().toLatin1().constData() << endl;
return 1;
}
QDataStream outputData(&outputFile);
Bitstream output(outputData, Bitstream::FULL_METADATA);
if (argc < 4) {
// default type is a single QVariant
QVariant value;
input >> value;
output << value;
} else {
for (int i = 3; i < argc; i++) {
int type = QMetaType::type(argv[i]);
if (type == QMetaType::UnknownType) {
cerr << "Unknown type: " << argv[i] << endl;
return 1;
}
const TypeStreamer* streamer = Bitstream::getTypeStreamer(type);
if (!streamer) {
cerr << "Non-streamable type: " << argv[i] << endl;
return 1;
}
QVariant value;
streamer->putJSONData(input, input.retrieveNextFromContents(), value);
streamer->write(output, value);
}
}
output.flush();
return 0;
}