Skip to content

Commit fa417f8

Browse files
authored
Merge pull request #4133 from mitza-oci/typesupport-data-conversions
Extend JSON data conversions via TypeSupport
2 parents be83ddb + a89caf1 commit fa417f8

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

dds/DCPS/TypeSupportImpl.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,12 @@ void TypeSupportImpl::get_type_from_type_lookup_service()
209209
}
210210
#endif
211211

212-
struct JsonRepresentationFormatImpl : JsonRepresentationFormat {
212+
struct JsonRepresentationFormatImpl
213+
: JsonRepresentationFormat, PoolAllocationBase {
213214
};
214215

215-
struct CdrRepresentationFormatImpl : CdrRepresentationFormat {
216+
struct CdrRepresentationFormatImpl
217+
: CdrRepresentationFormat, PoolAllocationBase {
216218
CdrRepresentationFormatImpl(DDS::DataRepresentationId_t)
217219
{}
218220
};

dds/idl/ts_generator.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -401,42 +401,68 @@ bool ts_generator::generate_ts(AST_Decl* node, UTL_ScopedName* name)
401401
" }\n"
402402
"#else\n"
403403
" ACE_UNUSED_ARG(in);\n"
404-
" ACE_UNUSED_ARG(out);\n"
405404
" ACE_UNUSED_ARG(format);\n"
406405
"#endif\n"
406+
" out = \"\";\n"
407407
" return ::DDS::RETCODE_UNSUPPORTED;\n"
408408
"}\n\n"
409409
"::DDS::ReturnCode_t " << ts_short_name << "TypeSupportImpl::encode_to_bytes(const " << short_name << "& in, ::DDS::OctetSeq_out out, OpenDDS::DCPS::RepresentationFormat* format)\n"
410410
"{\n"
411+
"#if OPENDDS_HAS_JSON_VALUE_WRITER\n"
412+
" OpenDDS::DCPS::JsonRepresentationFormat_var jrf = OpenDDS::DCPS::JsonRepresentationFormat::_narrow(format);\n"
413+
" if (jrf) {\n"
414+
" CORBA::String_var buffer;\n"
415+
" const ::DDS::ReturnCode_t ret = encode_to_string(in, buffer, format);\n"
416+
" const ::DDS::UInt32 len = static_cast< ::DDS::UInt32>(std::strlen(buffer));\n"
417+
" out = new ::DDS::OctetSeq(len);\n"
418+
" out->length(len);\n"
419+
" std::memcpy(out->get_buffer(), buffer, len);\n"
420+
" return ::DDS::RETCODE_OK;\n"
421+
" }\n"
422+
"#else\n"
411423
" ACE_UNUSED_ARG(in);\n"
412-
" ACE_UNUSED_ARG(out);\n"
413424
" ACE_UNUSED_ARG(format);\n"
425+
"#endif\n"
426+
" out = new ::DDS::OctetSeq();\n"
414427
" return ::DDS::RETCODE_UNSUPPORTED;\n"
415428
"}\n\n"
416429
"::DDS::ReturnCode_t " << ts_short_name << "TypeSupportImpl::decode_from_string(const char* in, " << short_name << "_out " <<
417430
(alloc_out ? "out" : "param") << ", OpenDDS::DCPS::RepresentationFormat* format)\n"
418-
"{\n"
431+
"{\n";
432+
433+
if (alloc_out) {
434+
be_global->impl_ << " out = new " << short_name << ";\n";
435+
} else {
436+
be_global->impl_ << " " << short_name << "* out = &param;\n";
437+
}
438+
439+
be_global->impl_ <<
440+
" OpenDDS::DCPS::set_default(*out);\n"
419441
"#if OPENDDS_HAS_JSON_VALUE_READER\n"
420442
" OpenDDS::DCPS::JsonRepresentationFormat_var jrf = OpenDDS::DCPS::JsonRepresentationFormat::_narrow(format);\n"
421443
" if (jrf) {\n"
422444
" rapidjson::StringStream buffer(in);\n"
423445
" OpenDDS::DCPS::JsonValueReader<> jvr(buffer);\n" <<
424-
(alloc_out ? " out = new " + short_name + ";\n" : " " + short_name + "* out = &param;\n") <<
425-
" OpenDDS::DCPS::set_default(*out);\n"
426446
" return vread(jvr, *out) ? ::DDS::RETCODE_OK : ::DDS::RETCODE_ERROR;\n"
427447
" }\n"
428448
"#else\n"
429449
" ACE_UNUSED_ARG(in);\n"
430-
" ACE_UNUSED_ARG(" << (alloc_out ? "out" : "param") << ");\n"
431450
" ACE_UNUSED_ARG(format);\n"
432451
"#endif\n"
433452
" return ::DDS::RETCODE_UNSUPPORTED;\n"
434453
"}\n\n"
435454
"::DDS::ReturnCode_t " << ts_short_name << "TypeSupportImpl::decode_from_bytes(const ::DDS::OctetSeq& in, " << short_name << "_out out, OpenDDS::DCPS::RepresentationFormat* format)\n"
436455
"{\n"
456+
"#if OPENDDS_HAS_JSON_VALUE_READER\n"
457+
" OpenDDS::DCPS::JsonRepresentationFormat_var jrf = OpenDDS::DCPS::JsonRepresentationFormat::_narrow(format);\n"
458+
" if (jrf) {\n"
459+
" return decode_from_string(reinterpret_cast<const char*>(in.get_buffer()), out, format);\n"
460+
" }\n"
461+
"#else\n"
437462
" ACE_UNUSED_ARG(in);\n"
438-
" ACE_UNUSED_ARG(out);\n"
439463
" ACE_UNUSED_ARG(format);\n"
464+
"#endif\n"
465+
" out = " << (alloc_out ? "new " : "") << short_name << "();\n"
440466
" return ::DDS::RETCODE_UNSUPPORTED;\n"
441467
"}\n\n"
442468
<< ts_short_name << "TypeSupport::_ptr_type " << ts_short_name << "TypeSupportImpl::_narrow(CORBA::Object_ptr obj)\n"

java/tests/vread_vwrite/VreadVwrite.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,40 @@ public static void main(String[] args) throws Exception {
2929
System.out.println("ERROR: round-trip JSON doesn't match: " + holder.value);
3030
System.exit(1);
3131
}
32+
33+
DDS.OctetSeqHolder bytesHolder = new DDS.OctetSeqHolder();
34+
ret = ts.encode_to_bytes(sample.value, bytesHolder, format);
35+
if (ret != DDS.RETCODE_OK.value) {
36+
System.out.println("ERROR: encode_to_bytes failed " + ret);
37+
System.exit(1);
38+
}
39+
40+
String bytesAsString = new String(bytesHolder.value, StandardCharsets.ISO_8859_1);
41+
if (bytesAsString.length() != holder.value.length()) {
42+
System.out.println("ERROR: byte[].length " + bytesAsString.length() + " doesn't match String: " + holder.value.length());
43+
System.exit(1);
44+
}
45+
if (!bytesAsString.equals(holder.value)) {
46+
System.out.println("ERROR: byte[] " + bytesAsString + " doesn't match String: " + holder.value);
47+
System.exit(1);
48+
}
49+
50+
SampleHolder sample2 = new SampleHolder();
51+
ret = ts.decode_from_bytes(bytesHolder.value, sample2, format);
52+
if (ret != DDS.RETCODE_OK.value) {
53+
System.out.println("ERROR: decode_from_bytes failed " + ret);
54+
System.exit(1);
55+
}
56+
57+
StringHolder holder2 = new StringHolder();
58+
ret = ts.encode_to_string(sample2.value, holder2, format);
59+
if (ret != DDS.RETCODE_OK.value) {
60+
System.out.println("ERROR: encode_to_string (sample2) failed " + ret);
61+
System.exit(1);
62+
}
63+
if (!holder.value.equals(holder2.value)) {
64+
System.out.println("ERROR: direct to string and byte[] to sample to string don't match");
65+
System.exit(1);
66+
}
3267
}
3368
}

tests/DCPS/Compiler/vread_vwrite/VreadVwriteTest.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ TEST(VreadVwriteTest, ParseTest)
2121
Mod::SampleTypeSupport_var ts = new Mod::SampleTypeSupportImpl;
2222
OpenDDS::DCPS::RepresentationFormat_var format = ts->make_format(OpenDDS::DCPS::JSON_DATA_REPRESENTATION);
2323
Mod::Sample_var samplev;
24-
ASSERT_EQ(ts->decode_from_string(stream.str().c_str(), samplev, format), DDS::RETCODE_OK);
25-
Mod::Sample& sample = *samplev;
24+
std::string as_string = stream.str();
25+
ASSERT_EQ(ts->decode_from_string(as_string.c_str(), samplev, format), DDS::RETCODE_OK);
26+
const Mod::Sample sample = *samplev;
27+
28+
const unsigned int len = static_cast<unsigned int>(as_string.size());
29+
DDS::OctetSeq octets(len, len, reinterpret_cast<unsigned char*>(&as_string[0]));
30+
ASSERT_EQ(ts->decode_from_bytes(octets, samplev, format), DDS::RETCODE_OK);
31+
const Mod::Sample sample2 = *samplev;
32+
// sample2 should be exactly the same as sample, a few of the members are checked below
2633

2734
ASSERT_EQ(sample.id, 5);
35+
ASSERT_EQ(sample2.id, 5);
2836
ASSERT_EQ(std::string(sample.data.in()), "The most rapid of JSONs");
37+
ASSERT_EQ(std::string(sample2.data.in()), "The most rapid of JSONs");
2938
ASSERT_EQ(sample.enu, Mod::three);
39+
ASSERT_EQ(sample2.enu, Mod::three);
3040
ASSERT_EQ(sample.enu2, Mod::two);
41+
ASSERT_EQ(sample2.enu2, Mod::two);
3142
ASSERT_EQ(sample.bt.o, 129);
3243
#if OPENDDS_HAS_EXPLICIT_INTS
3344
ASSERT_EQ(sample.bt.u8, 130);
@@ -166,6 +177,12 @@ TEST(VreadVwriteTest, SerializeTest)
166177
CORBA::String_var buffer;
167178
ASSERT_EQ(DDS::RETCODE_OK, ts->encode_to_string(sample, buffer, format));
168179

180+
DDS::OctetSeq_var bytes;
181+
ASSERT_EQ(DDS::RETCODE_OK, ts->encode_to_bytes(sample, bytes, format));
182+
const unsigned int n = bytes->length();
183+
ASSERT_EQ(n, std::strlen(buffer));
184+
ASSERT_EQ(0, std::memcmp(buffer.in(), bytes->get_buffer(), n));
185+
169186
// Then parse.
170187
rapidjson::Document document;
171188
document.Parse(buffer.in());

0 commit comments

Comments
 (0)