Mercurial > hg > orthanc
comparison Core/Toolbox.cpp @ 1045:0bfeeb6d340f
json to xml conversion with pugixml
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 18 Jul 2014 16:41:10 +0200 |
parents | 509e146c3cb3 |
children | 64f1842aae2e |
comparison
equal
deleted
inserted
replaced
1044:6d90e2bcab60 | 1045:0bfeeb6d340f |
---|---|
81 } | 81 } |
82 } | 82 } |
83 #endif | 83 #endif |
84 | 84 |
85 | 85 |
86 #if ORTHANC_PUGIXML_ENABLED == 1 | |
87 #include "ChunkedBuffer.h" | |
88 #include <pugixml.hpp> | |
89 #endif | |
90 | |
91 | |
86 namespace Orthanc | 92 namespace Orthanc |
87 { | 93 { |
88 static bool finish; | 94 static bool finish; |
89 | 95 |
90 #if defined(_WIN32) | 96 #if defined(_WIN32) |
822 bool Toolbox::IsExistingFile(const std::string& path) | 828 bool Toolbox::IsExistingFile(const std::string& path) |
823 { | 829 { |
824 return boost::filesystem::exists(path); | 830 return boost::filesystem::exists(path); |
825 } | 831 } |
826 | 832 |
833 | |
834 #if ORTHANC_PUGIXML_ENABLED == 1 | |
835 class ChunkedBufferWriter : public pugi::xml_writer | |
836 { | |
837 private: | |
838 ChunkedBuffer buffer_; | |
839 | |
840 public: | |
841 virtual void write(const void *data, size_t size) | |
842 { | |
843 if (size > 0) | |
844 { | |
845 buffer_.AddChunk(reinterpret_cast<const char*>(data), size); | |
846 } | |
847 } | |
848 | |
849 void Flatten(std::string& s) | |
850 { | |
851 buffer_.Flatten(s); | |
852 } | |
853 }; | |
854 | |
855 | |
856 static void JsonToXmlInternal(pugi::xml_node& target, | |
857 const Json::Value& source, | |
858 const std::string& arrayElement) | |
859 { | |
860 // http://jsoncpp.sourceforge.net/value_8h_source.html#l00030 | |
861 | |
862 switch (source.type()) | |
863 { | |
864 case Json::nullValue: | |
865 { | |
866 target.append_child(pugi::node_pcdata).set_value("null"); | |
867 break; | |
868 } | |
869 | |
870 case Json::intValue: | |
871 { | |
872 std::string s = boost::lexical_cast<std::string>(source.asInt()); | |
873 target.append_child(pugi::node_pcdata).set_value(s.c_str()); | |
874 break; | |
875 } | |
876 | |
877 case Json::uintValue: | |
878 { | |
879 std::string s = boost::lexical_cast<std::string>(source.asUInt()); | |
880 target.append_child(pugi::node_pcdata).set_value(s.c_str()); | |
881 break; | |
882 } | |
883 | |
884 case Json::realValue: | |
885 { | |
886 std::string s = boost::lexical_cast<std::string>(source.asFloat()); | |
887 target.append_child(pugi::node_pcdata).set_value(s.c_str()); | |
888 break; | |
889 } | |
890 | |
891 case Json::stringValue: | |
892 { | |
893 target.append_child(pugi::node_pcdata).set_value(source.asString().c_str()); | |
894 break; | |
895 } | |
896 | |
897 case Json::booleanValue: | |
898 { | |
899 target.append_child(pugi::node_pcdata).set_value(source.asBool() ? "true" : "false"); | |
900 break; | |
901 } | |
902 | |
903 case Json::arrayValue: | |
904 { | |
905 for (Json::Value::ArrayIndex i = 0; i < source.size(); i++) | |
906 { | |
907 pugi::xml_node node = target.append_child(); | |
908 node.set_name(arrayElement.c_str()); | |
909 JsonToXmlInternal(node, source[i], arrayElement); | |
910 } | |
911 break; | |
912 } | |
913 | |
914 case Json::objectValue: | |
915 { | |
916 Json::Value::Members members = source.getMemberNames(); | |
917 | |
918 for (size_t i = 0; i < members.size(); i++) | |
919 { | |
920 pugi::xml_node node = target.append_child(); | |
921 node.set_name(members[i].c_str()); | |
922 JsonToXmlInternal(node, source[members[i]], arrayElement); | |
923 } | |
924 | |
925 break; | |
926 } | |
927 | |
928 default: | |
929 throw OrthancException(ErrorCode_NotImplemented); | |
930 } | |
931 } | |
932 | |
933 | |
934 void Toolbox::JsonToXml(std::string& target, | |
935 const Json::Value& source, | |
936 const std::string& rootElement, | |
937 const std::string& arrayElement) | |
938 { | |
939 pugi::xml_document doc; | |
940 | |
941 pugi::xml_node n = doc.append_child(rootElement.c_str()); | |
942 JsonToXmlInternal(n, source, arrayElement); | |
943 | |
944 pugi::xml_node decl = doc.prepend_child(pugi::node_declaration); | |
945 decl.append_attribute("version").set_value("1.0"); | |
946 decl.append_attribute("encoding").set_value("utf-8"); | |
947 | |
948 ChunkedBufferWriter writer; | |
949 doc.save(writer, " ", pugi::format_default, pugi::encoding_utf8); | |
950 writer.Flatten(target); | |
951 } | |
952 | |
953 #endif | |
827 } | 954 } |
828 | 955 |