Skip to content

Commit ac9dd6e

Browse files
author
Florian Thake
committed
CORELIB|JSON: added experimental BSON support if nlohmann::json Adapter is used.
- added readbsonbuffer and writebsonbuffer to core library for ... ... Tuple <--> BSON Buffer conversion.
1 parent 3eb693e commit ac9dd6e

5 files changed

Lines changed: 72 additions & 0 deletions

File tree

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ NOTE: The dates are not the release dates, but a last commit date.
88
================
99
VERSION 0.16.0 (not released yet, pre-release)
1010
================
11+
CORELIB|JSON: added experimental BSON support if nlohmann::json Adapter is used.
12+
- added readbsonbuffer and writebsonbuffer to core library for ...
13+
... Tuple <--> BSON Buffer conversion.
14+
1115
CLEAN: finally removed deprecated CoreLibrary::ExitScript().
1216
- Please, use _Exit val instead or directly throw teascript::control::Exit_Script from C++.
1317

extensions/include/teascript/ext/JsonAdapterNlohmann.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class JsonAdapterNlohmann
4040
static ValueObject ToValueObject( Context &rContext, JsonType const &json );
4141

4242
static void FromValueObject( ValueObject const &rObj, JsonType &rOut );
43+
44+
45+
// nlohmann supports different binary formats. We support BSON here.
46+
47+
static ValueObject FromBSON( Context &rContext, Buffer const &rBuffer );
48+
static ValueObject ToBSON( ValueObject const &rObj );
4349
};
4450

4551
} // namespace teascript

extensions/source/JsonAdapterNlohmann.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,25 @@ void JsonAdapterNlohmann::FromValueObject( ValueObject const &rObj, JsonType & r
158158
}
159159
}
160160

161+
ValueObject JsonAdapterNlohmann::FromBSON( Context &rContext, Buffer const &rBuffer )
162+
{
163+
auto json = nlohmann::json::from_bson( rBuffer );
164+
return ToValueObject( rContext, json );
165+
}
166+
167+
ValueObject JsonAdapterNlohmann::ToBSON( ValueObject const &rObj )
168+
{
169+
JsonType json;
170+
try {
171+
FromValueObject( rObj, json );
172+
} catch( std::exception const & ) {
173+
// TODO better error handling! Use Error once it exist!
174+
return ValueObject( false );
175+
}
176+
177+
Buffer bson = nlohmann::json::to_bson( json );
178+
return ValueObject( std::move( bson ) );
179+
}
180+
181+
161182
} // namespace teascript

include/teascript/CoreLibrary.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,6 +2066,23 @@ class CoreLibrary
20662066
ValueObject val{std::move( func ), cfg_mutable};
20672067
tea_add_var( "json_array_remove", std::move( val ) ); // missing _ is intended
20682068
}
2069+
2070+
// EXPERIMENTAL BSON support (nlohmann adapter must be used!)
2071+
# if TEASCRIPT_BSONSUPPORT
2072+
// readbsonbuffer : Any ( Buffer ) --> creates a value of appropriate type from the given BSON buffer (or a TypeInfo on error).
2073+
{
2074+
auto func = std::make_shared< LibraryFunction< decltype(JsonSupport<>::ReadBsonBuffer) > >( &JsonSupport<>::ReadBsonBuffer );
2075+
ValueObject val{std::move( func ), cfg_mutable};
2076+
tea_add_var( "readbsonbuffer", std::move( val ) ); // missing _ is intended
2077+
}
2078+
2079+
// writebsonbuffer : Buffer|Bool ( Any ) --> creates a Bson buffer (or false on error).
2080+
{
2081+
auto func = std::make_shared< LibraryFunction< decltype(JsonSupport<>::WriteBsonBuffer) > >( &JsonSupport<>::WriteBsonBuffer );
2082+
ValueObject val{std::move( func ), cfg_mutable};
2083+
tea_add_var( "writebsonbuffer", std::move( val ) ); // missing _ is intended
2084+
}
2085+
# endif
20692086
#endif
20702087
}
20712088

include/teascript/JsonSupport.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ namespace teascript {
4646
using JsonAdapter = JsonAdapterPico;
4747
}
4848
#elif TEASCRIPT_JSONSUPPORT == TEASCRIPT_JSON_NLOHMANN
49+
# if !defined( TEASCRIPT_BSONSUPPORT)
50+
// nlohmann supports BSON
51+
# define TEASCRIPT_BSONSUPPORT 1
52+
# endif
4953
#if __has_include("teascript/ext/JsonAdapterNlohmann.hpp")
5054
# include "teascript/ext/JsonAdapterNlohmann.hpp"
5155
#else
@@ -76,6 +80,9 @@ using JsonAdapter = JsonAdapterBoost;
7680
# error unsupported JSON flavor for TeaScript
7781
#endif
7882

83+
#if !defined( TEASCRIPT_BSONSUPPORT)
84+
# define TEASCRIPT_BSONSUPPORT 0
85+
#endif
7986

8087
namespace teascript {
8188

@@ -117,6 +124,23 @@ class JsonSupport
117124
{
118125
Adapter::FromValueObject( rObj, rOut );
119126
}
127+
128+
// EXPERIMENTAL BSON support (nlohmann adapter must be used!)
129+
#if TEASCRIPT_BSONSUPPORT
130+
/// Constructs a ValueObject structure from the given BSON buffer.
131+
static ValueObject ReadBsonBuffer( Context &rContext, Buffer const &rBuffer )
132+
{
133+
return Adapter::FromBSON( rContext, rBuffer );
134+
}
135+
136+
/// Constructs a Bson buffer from the given ValueObject.
137+
/// \return the constructed buffer or false on error.
138+
/// \note the object must only contain supported types and layout for Json.
139+
static ValueObject WriteBsonBuffer( ValueObject const &rObj )
140+
{
141+
return Adapter::ToBSON( rObj );
142+
}
143+
#endif
120144
};
121145

122146
} // namespace teascript

0 commit comments

Comments
 (0)