add support for serializing to xml
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2008-2022, Tomasz Sowa
|
||||
* Copyright (c) 2008-2023, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -62,6 +62,9 @@ Request::Request()
|
||||
plugin = nullptr;
|
||||
mounts = nullptr;
|
||||
|
||||
json_connector = nullptr;
|
||||
xml_connector = nullptr;
|
||||
|
||||
// set function to nullptr because is used in Clear()
|
||||
function = nullptr;
|
||||
}
|
||||
@@ -111,6 +114,18 @@ void Request::SetMounts(Mounts * mounts)
|
||||
}
|
||||
|
||||
|
||||
void Request::set_json_connector(morm::JSONConnector * json_connector)
|
||||
{
|
||||
this->json_connector = json_connector;
|
||||
}
|
||||
|
||||
|
||||
void Request::set_xml_connector(morm::XMLConnector * xml_connector)
|
||||
{
|
||||
this->xml_connector = xml_connector;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Request::ClearOutputStreams()
|
||||
{
|
||||
@@ -933,7 +948,7 @@ void Request::PrepareJsonAnswer()
|
||||
|
||||
void Request::PrepareXmlAnswer()
|
||||
{
|
||||
output_8bit << '<';
|
||||
output_8bit << "<?xml version=\"1.0\" encoding=\"UTF-8\"?><";
|
||||
pt::esc_to_xml(config->xml_root, output_8bit);
|
||||
output_8bit << '>';
|
||||
|
||||
@@ -982,8 +997,10 @@ void Request::PrepareContenerizedAnswer()
|
||||
if( send_all_frames || !send_frames.empty() )
|
||||
{
|
||||
PutSeparatorIfNeeded(put_separator);
|
||||
SerializeFieldJson(config->ezc_frames_field.c_str());
|
||||
output_8bit << "{";
|
||||
SerializeField(config->ezc_frames_field.c_str());
|
||||
|
||||
if( container_type == Request::ContainerType::container_json )
|
||||
output_8bit << '{';
|
||||
|
||||
if( send_all_frames )
|
||||
{
|
||||
@@ -995,7 +1012,10 @@ void Request::PrepareContenerizedAnswer()
|
||||
SerializeSpecificFrames();
|
||||
}
|
||||
|
||||
output_8bit << "}";
|
||||
if( container_type == Request::ContainerType::container_json )
|
||||
output_8bit << '}';
|
||||
|
||||
SerializeClosingField(config->ezc_frames_field.c_str());
|
||||
put_separator = true;
|
||||
}
|
||||
}
|
||||
@@ -1008,14 +1028,14 @@ void Request::PutSeparatorIfNeeded(bool put_separator)
|
||||
switch( container_type )
|
||||
{
|
||||
case Request::ContainerType::container_json:
|
||||
output_8bit << ",";
|
||||
output_8bit << ',';
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_xml:
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_csv:
|
||||
output_8bit << ";";
|
||||
output_8bit << ';';
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_raw:
|
||||
@@ -1026,6 +1046,50 @@ void Request::PutSeparatorIfNeeded(bool put_separator)
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeField(const wchar_t * field_name)
|
||||
{
|
||||
switch( container_type )
|
||||
{
|
||||
case Request::ContainerType::container_json:
|
||||
SerializeFieldJson(field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_xml:
|
||||
SerializeFieldXml(field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_csv:
|
||||
SerializeFieldCsv(field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_raw:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeClosingField(const wchar_t * field_name)
|
||||
{
|
||||
switch( container_type )
|
||||
{
|
||||
case Request::ContainerType::container_json:
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_xml:
|
||||
SerializeClosingFieldXml(field_name);
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_csv:
|
||||
break;
|
||||
|
||||
case Request::ContainerType::container_raw:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeFieldJson(const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
@@ -1037,6 +1101,33 @@ void Request::SerializeFieldJson(const wchar_t * field_name)
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeFieldXml(const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << '<';
|
||||
pt::esc_to_xml(field_name, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeClosingFieldXml(const wchar_t * field_name)
|
||||
{
|
||||
if( field_name )
|
||||
{
|
||||
output_8bit << "</";
|
||||
pt::esc_to_xml(field_name, output_8bit);
|
||||
output_8bit << '>';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeFieldCsv(const wchar_t * field_name)
|
||||
{
|
||||
// IMPLEMENTME
|
||||
}
|
||||
|
||||
|
||||
void Request::SerializeStream(const pt::WTextStream & input_stream, const wchar_t * field_name)
|
||||
{
|
||||
@@ -1141,10 +1232,7 @@ void Request::SerializeAllFrames()
|
||||
|
||||
for( ; i != out_streams.streams_map.end() ; ++i)
|
||||
{
|
||||
if( container_type == Request::ContainerType::container_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
PutSeparatorIfNeeded(!is_first);
|
||||
|
||||
if( container_type == Request::ContainerType::container_xml && i->first.empty() )
|
||||
{
|
||||
@@ -1175,11 +1263,7 @@ void Request::SerializeSpecificFrames()
|
||||
|
||||
if( i != out_streams.streams_map.end() )
|
||||
{
|
||||
if( container_type == Request::ContainerType::container_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
|
||||
PutSeparatorIfNeeded(!is_first);
|
||||
SerializeStream(i->second->get_buffer(), frame.c_str());
|
||||
is_first = false;
|
||||
}
|
||||
@@ -1204,10 +1288,7 @@ void Request::SerializeModels()
|
||||
|
||||
for( ; i != models_map.end() ; ++i)
|
||||
{
|
||||
if( container_type == Request::ContainerType::container_json && !is_first )
|
||||
{
|
||||
output_8bit << ',';
|
||||
}
|
||||
PutSeparatorIfNeeded(!is_first);
|
||||
|
||||
if( container_type == Request::ContainerType::container_xml && i->first.empty() )
|
||||
{
|
||||
@@ -1303,13 +1384,60 @@ void Request::SerializeModelJson(morm::Wrapper & wrapper, const wchar_t * field_
|
||||
|
||||
void Request::SerializeModelXml(morm::Wrapper & wrapper, const wchar_t * field_name)
|
||||
{
|
||||
// IMPROVEME
|
||||
Log * plog = get_logger();
|
||||
SerializeFieldXml(field_name);
|
||||
|
||||
if( plog )
|
||||
if( wrapper.model )
|
||||
{
|
||||
(*plog) << log2 << "Request: serializing models to xml not implemented yet" << logend;
|
||||
serialized_model.clear();
|
||||
wrapper.model->set_connector(model_connector);
|
||||
model_connector->set_flat_connector(*xml_connector);
|
||||
wrapper.model->to_text(serialized_model);
|
||||
model_connector->set_flat_connector(*json_connector);
|
||||
output_8bit << serialized_model;
|
||||
}
|
||||
|
||||
if( wrapper.date )
|
||||
{
|
||||
output_8bit << "<date>";
|
||||
wrapper.date->SerializeISO(output_8bit);
|
||||
output_8bit << "</date>";
|
||||
}
|
||||
|
||||
if( wrapper.space_wrapper )
|
||||
{
|
||||
serialized_model.clear();
|
||||
wrapper.space_wrapper->get_space()->serialize_to_json_stream(serialized_model, false);
|
||||
pt::TextStream::iterator i = serialized_model.begin();
|
||||
|
||||
for( ; i != serialized_model.end() ; ++i)
|
||||
{
|
||||
pt::esc_to_xml(*i, output_8bit);
|
||||
}
|
||||
}
|
||||
|
||||
if( wrapper.model_container_wrapper )
|
||||
{
|
||||
wrapper.model_container_wrapper->set_iterator_at_first_model();
|
||||
size_t index = 0;
|
||||
|
||||
while( wrapper.model_container_wrapper->is_iterator_correct() )
|
||||
{
|
||||
output_8bit << "<item index=\"" << index << "\">";
|
||||
morm::Model * model = wrapper.model_container_wrapper->get_model();
|
||||
serialized_model.clear();
|
||||
model->set_connector(model_connector);
|
||||
model_connector->set_flat_connector(*xml_connector);
|
||||
model->to_text(serialized_model);
|
||||
model_connector->set_flat_connector(*json_connector);
|
||||
output_8bit << serialized_model;
|
||||
|
||||
wrapper.model_container_wrapper->increment_iterator();
|
||||
output_8bit << "</item>";
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
SerializeClosingFieldXml(field_name);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user