Skip to content

Commit

Permalink
read_as (#78)
Browse files Browse the repository at this point in the history
* read_as

changed write_from to read_as

* fix eigen_test

* removed extra decay_t
  • Loading branch information
stephenberry authored Dec 12, 2022
1 parent 9295732 commit e804eec
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 44 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,22 @@ glz::set(tuple, "/0", 5);
expect(std::get<0>(tuple) == 5.0);
```

### write_from
### read_as

`write_from` allows you to write to a JSON pointer via a JSON input buffer.
`read_as` allows you to read into an object from a JSON pointer and an input buffer.

```c++
Thing thing{};
glz::write_from(thing, "/vec3", "[7.6, 1292.1, 0.333]");
glz::read_as_json(thing, "/vec3", "[7.6, 1292.1, 0.333]");
expect(thing.vec3.x == 7.6 && thing.vec3.y == 1292.1 &&
thing.vec3.z == 0.333);

glz::write_from(thing, "/vec3/2", "999.9");
glz::read_as_json(thing, "/vec3/2", "999.9");
expect(thing.vec3.z == 999.9);
```
`read_as_binary` is also supported.
## JSON With Comments (JSONC)
Comments are supported with the specification defined here: [JSONC](https://github.com/stephenberry/JSONC)
Expand Down
1 change: 1 addition & 0 deletions include/glaze/binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
#include "glaze/binary/header.hpp"
#include "glaze/binary/read.hpp"
#include "glaze/binary/write.hpp"
#include "glaze/binary/ptr.hpp"
22 changes: 22 additions & 0 deletions include/glaze/binary/ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Glaze Library
// For the license information refer to glaze.hpp

#pragma once

#include "glaze/core/ptr.hpp"
#include "glaze/binary/read.hpp"
#include "glaze/binary/write.hpp"

namespace glz
{
template <class T, class B>
bool read_as_binary(T&& root_value, const sv json_ptr, B&& buffer) {
return read_as<opts{.format = binary}>(std::forward<T>(root_value), json_ptr, buffer);
}

template <class T, class B>
bool write_as_binary(T&& root_value, const sv json_ptr, B& buffer)
{
return write_as<opts{.format = binary}>(std::forward<T>(root_value), json_ptr, buffer);
}
}
38 changes: 38 additions & 0 deletions include/glaze/core/ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Glaze Library
// For the license information refer to glaze.hpp

#pragma once

#include "glaze/core/opts.hpp"
#include "glaze/core/read.hpp"
#include "glaze/core/write.hpp"
#include "glaze/util/for_each.hpp"
#include "glaze/json/json_ptr.hpp"

namespace glz
{
// Given a JSON pointer path, reads from the buffer into the object
template <opts Opts, class T, class B>
bool read_as(T&& root_value, const sv json_ptr, B&& buffer) {
return detail::seek_impl(
[&](auto&& val) {
read<Opts>(val, buffer);
},
std::forward<T>(root_value), json_ptr
);
}

// Given a JSON pointer path, find the value in the buffer and reads only that sub value
/*template <opts Opts, class T, class B>
bool read_sub(T&& root_value, const sv json_ptr, B&& buffer) {
}*/

// Given a JSON pointer path, writes into a buffer the specified value
template <opts Opts, class T, class B>
bool write_as(T&& root_value, const sv json_ptr, B& buffer)
{
return detail::seek_impl([&](auto&& val) { write<Opts>(val, buffer); },
std::forward<T>(root_value), json_ptr);
}
}
2 changes: 1 addition & 1 deletion include/glaze/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
#include "glaze/json/read.hpp"
#include "glaze/json/write.hpp"
#include "glaze/json/json_ptr.hpp"
#include "glaze/json/from_ptr.hpp"
#include "glaze/json/ptr.hpp"
#include "glaze/json/prettify.hpp"
30 changes: 0 additions & 30 deletions include/glaze/json/from_ptr.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions include/glaze/json/json_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace glz
namespace detail
{
template <class F, class T>
requires glaze_array_t<std::decay_t<T>> || tuple_t<std::decay_t<T>> || array_t<std::decay_t<T>> ||
requires glaze_array_t<T> || tuple_t<std::decay_t<T>> || array_t<std::decay_t<T>> ||
is_std_tuple<std::decay_t<T>>
bool seek_impl(F&& func, T&& value, sv json_ptr);

Expand Down Expand Up @@ -122,7 +122,7 @@ namespace glz
}

template <class F, class T>
requires glaze_array_t<std::decay_t<T>> || tuple_t<std::decay_t<T>> || array_t<std::decay_t<T>> ||
requires glaze_array_t<T> || tuple_t<std::decay_t<T>> || array_t<std::decay_t<T>> ||
is_std_tuple<std::decay_t<T>>
bool seek_impl(F&& func, T&& value, sv json_ptr)
{
Expand Down
22 changes: 22 additions & 0 deletions include/glaze/json/ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Glaze Library
// For the license information refer to glaze.hpp

#pragma once

#include "glaze/core/ptr.hpp"
#include "glaze/json/read.hpp"
#include "glaze/json/write.hpp"

namespace glz
{
template <class T, class B>
bool read_as_json(T&& root_value, const sv json_ptr, B&& buffer) {
return read_as<opts{}>(std::forward<T>(root_value), json_ptr, buffer);
}

template <class T, class B>
bool write_as_json(T&& root_value, const sv json_ptr, B& buffer)
{
return write_as<opts{}>(std::forward<T>(root_value), json_ptr, buffer);
}
}
2 changes: 1 addition & 1 deletion include/glaze/json/study.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace glz
void overwrite(auto& state, const std::unordered_map<std::string, raw_json> &overwrites)
{
for (auto&& [json_ptr, raw_json_str] : overwrites) {
write_from(state, json_ptr, raw_json_str.str);
read_as_json(state, json_ptr, raw_json_str.str);
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/glaze/json/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "glaze/core/format.hpp"
#include "glaze/util/for_each.hpp"
#include "glaze/util/dump.hpp"
#include "glaze/json/from_ptr.hpp"
#include "glaze/json/ptr.hpp"

#include "glaze/util/to_chars.hpp"
#include "glaze/util/itoa.hpp"
Expand Down
2 changes: 1 addition & 1 deletion tests/eigen_test/eigen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "boost/ut.hpp"
#include "glaze/json/json_ptr.hpp"
#include "glaze/json/from_ptr.hpp"
#include "glaze/json/ptr.hpp"
#include "glaze/json/read.hpp"
#include "glaze/json/write.hpp"
#include "glaze/ext/eigen.hpp"
Expand Down
8 changes: 4 additions & 4 deletions tests/json_test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "glaze/core/macros.hpp"
#include "boost/ut.hpp"
#include "glaze/json/json_ptr.hpp"
#include "glaze/json/from_ptr.hpp"
#include "glaze/json/ptr.hpp"
#include "glaze/json/read.hpp"
#include "glaze/json/write.hpp"
#include "glaze/json/prettify.hpp"
Expand Down Expand Up @@ -684,13 +684,13 @@ void json_pointer() {
expect(std::get<2>(tuple) == "fish");
};

"overwrite"_test = [] {
"read_as_json"_test = [] {
Thing thing{};
glz::write_from(thing, "/vec3", "[7.6, 1292.1, 0.333]");
glz::read_as_json(thing, "/vec3", "[7.6, 1292.1, 0.333]");
expect(thing.vec3.x == 7.6 && thing.vec3.y == 1292.1 &&
thing.vec3.z == 0.333);

glz::write_from(thing, "/vec3/2", "999.9");
glz::read_as_json(thing, "/vec3/2", "999.9");
expect(thing.vec3.z == 999.9);
};

Expand Down

0 comments on commit e804eec

Please sign in to comment.