Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration guide #3702

Closed
wants to merge 61 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
a34d18f
:memo: add migration guide
nlohmann Aug 14, 2022
933a80e
Apply suggestions from code review
nlohmann Aug 17, 2022
8a59ae0
:ok_hand: address review comments
nlohmann Aug 20, 2022
2c90294
:loud_sound: add debug output
nlohmann Aug 20, 2022
90a81c7
:rewind: oops
nlohmann Aug 20, 2022
4c8cdd7
Add 'Check amalgamation' workflow (#3693)
falbrechtskirchinger Aug 27, 2022
f7973f4
Use official Clang/GCC containers (#3703)
nlohmann Aug 27, 2022
bed648c
Allow custom base class as node customization point (#3110)
barcode Aug 28, 2022
307c053
Add missing <numeric> include (#3719)
falbrechtskirchinger Aug 30, 2022
58bd97e
Add clang-tools to required tools for ci_static_analysis_clang (#3724)
nlohmann Sep 13, 2022
15ca78a
Add dark mode toggle to documentation (#3726)
falbrechtskirchinger Sep 15, 2022
c920f3f
Update Codacy link (#3740)
nlohmann Sep 16, 2022
9dfa722
Fix typos in .md files (#3748)
tocic Sep 17, 2022
2d1f9b6
Add Bazel build support (#3709)
Vertexwahn Sep 18, 2022
3d1252b
Replace limit macros with std::numeric_limits (#3723)
falbrechtskirchinger Sep 19, 2022
f4658de
Fix 'declaration hides global declaration' warning (#3751)
falbrechtskirchinger Sep 19, 2022
17d9eac
Add missing files to release artifacts (#3728)
falbrechtskirchinger Sep 25, 2022
4b2c8ce
Fix some typos for n-dimensional arrays (#3767)
striezel Sep 26, 2022
a3e6e26
Bump joblib from 1.1.0 to 1.2.0 in /docs/mkdocs (#3781)
dependabot[bot] Oct 2, 2022
e1cdbec
:memo: add migration guide
nlohmann Aug 14, 2022
865c36d
Apply suggestions from code review
nlohmann Aug 17, 2022
9f31c91
:ok_hand: address review comments
nlohmann Aug 20, 2022
be77d8c
:loud_sound: add debug output
nlohmann Aug 20, 2022
f1d65a3
:rewind: oops
nlohmann Aug 20, 2022
c6f8561
:ok_hand: apply suggestion
nlohmann Oct 2, 2022
c3c75e6
Merge remote-tracking branch 'origin/migration_guide' into migration_…
nlohmann Oct 2, 2022
24ba54e
Fix pipeline (#3862)
nlohmann Dec 4, 2022
d8e2680
:memo: add migration guide
nlohmann Aug 14, 2022
b73c342
Apply suggestions from code review
nlohmann Aug 17, 2022
5219a58
:ok_hand: address review comments
nlohmann Aug 20, 2022
96d024b
:loud_sound: add debug output
nlohmann Aug 20, 2022
c156278
:rewind: oops
nlohmann Aug 20, 2022
7836f57
:ok_hand: apply suggestion
nlohmann Oct 2, 2022
e15aad9
:memo: add migration guide
nlohmann Aug 14, 2022
1b779a7
Apply suggestions from code review
nlohmann Aug 17, 2022
52fd890
:memo: cleanup
nlohmann Dec 4, 2022
9a24def
:loud_sound: add debug output
nlohmann Aug 20, 2022
8343707
:rewind: oops
nlohmann Aug 20, 2022
2e14b20
Merge remote-tracking branch 'origin/migration_guide' into migration_…
nlohmann Dec 4, 2022
1bbbc96
:bug: fix natvis XML
nlohmann Dec 4, 2022
6289c5c
Fix natvis XML (#3863)
nlohmann Dec 9, 2022
918810a
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
c321134
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
21cc96a
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
559c0ff
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
682bdd9
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
3dfbbf5
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
6140ae1
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
293d2f0
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
9eca81f
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
3c50044
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
216cf06
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
e5dd786
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
82f24f4
:green_heart: fix Ubuntu build
nlohmann Dec 10, 2022
13f3f9a
:memo: add migration guide
nlohmann Aug 14, 2022
d418962
Apply suggestions from code review
nlohmann Aug 17, 2022
e94bdb0
:memo: add migration guide
nlohmann Aug 14, 2022
983588a
Apply suggestions from code review
nlohmann Aug 17, 2022
4db19ed
Bump certifi from 2022.6.15 to 2022.12.7 in /docs/mkdocs (#3872)
dependabot[bot] Dec 11, 2022
fbd7ca3
Add CIFuzz CI GitHub action (#3845)
DavidKorczynski Dec 11, 2022
ac6adbf
Clang 15 (#3876)
nlohmann Dec 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
📝 add migration guide
  • Loading branch information
nlohmann committed Dec 4, 2022
commit d8e2680abcc7649b78f992945b87511df4318c4b
2 changes: 1 addition & 1 deletion .github/workflows/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: srvaroa/labeler@master
- uses: falbrechtskirchinger/labeler@master
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
1 change: 1 addition & 0 deletions docs/docset/docSet.sql
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ INSERT INTO searchIndex(name, type, path) VALUES ('Element Access', 'Guide', 'fe
INSERT INTO searchIndex(name, type, path) VALUES ('Element Access: Access with default value: value', 'Guide', 'features/element_access/default_value/index.html');
INSERT INTO searchIndex(name, type, path) VALUES ('Element Access: Checked access: at', 'Guide', 'features/element_access/checked_access/index.html');
INSERT INTO searchIndex(name, type, path) VALUES ('Element Access: Unchecked access: operator[]', 'Guide', 'features/element_access/unchecked_access/index.html');
INSERT INTO searchIndex(name, type, path) VALUES ('Integration: Migration Guide', 'Guide', 'integration/migration_guide/index.html');
INSERT INTO searchIndex(name, type, path) VALUES ('Integration: CMake', 'Guide', 'integration/cmake/index.html');
INSERT INTO searchIndex(name, type, path) VALUES ('Integration: Header only', 'Guide', 'integration/index.html');
INSERT INTO searchIndex(name, type, path) VALUES ('Integration: Package Managers', 'Guide', 'integration/package_managers/index.html');
Expand Down
248 changes: 248 additions & 0 deletions docs/mkdocs/docs/integration/migration_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# Migration Guide

This page collects some guidelines how to future-proof your code for future versions of this library.
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

## Replace deprecated functions

The following functions have been deprecated in earlier versions and will be removed in the next major version (i.e.,
4.0.0). All deprecations are annotated with
[`HEDLEY_DEPRECATED_FOR`](https://nemequ.github.io/hedley/api-reference.html#HEDLEY_DEPRECATED_FOR) to report which
function to use instead.
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

#### Parsing

- Function `friend std::istream& operator<<(basic_json&, std::istream&)` is deprecated since 3.0.0. Please use
[`friend std::istream& operator>>(std::istream&, basic_json&)`](../api/operator_gtgt.md) instead.

=== "Deprecated"

```cpp
nlohmann::json j;
std::stringstream ss("[1,2,3]");
j << ss;
```

=== "Future-proof"

```cpp
nlohmann::json j;
std::stringstream ss("[1,2,3]");
ss >> j;
```

- Passing iterator pairs or pointer/length pairs to parsing functions ([`parse`](../api/basic_json/parse.md),
[`accept`](../api/basic_json/accept.md), [`sax_parse`](../api/basic_json/sax_parse.md),
[`from_cbor`](../api/basic_json/from_cbor.md), [`from_msgpack`](../api/basic_json/from_msgpack.md),
[`from_ubjson`](../api/basic_json/from_ubjson.md), and [`from_bson`](../api/basic_json/from_bson.md) via initializer
lists is deprecated since 3.8.0. Instead, pass two iterators; for instance, call `from_cbor(ptr, ptr+len)` instead of
`from_cbor({ptr, len})`.

=== "Deprecated"

```cpp
const char* s = "[1,2,3]";
bool ok = nlohmann::json::accept({s, s + std::strlen(s)});
```

=== "Future-proof"

```cpp
const char* s = "[1,2,3]";
bool ok = nlohmann::json::accept(s, s + std::strlen(s));
```

#### JSON Pointers

- Comparing JSON Pointers with strings via [`operator==`](../api/json_pointer/operator_eq.md) and
[`operator!=`](../api/json_pointer/operator_ne.md) is deprecated since 3.11.2. To compare a
[`json_pointer`](../api/json_pointer/index.md) `p` with a string `s`, convert `s` to a `json_pointer` first and use
[`json_pointer::operator==`](../api/json_pointer/operator_eq.md) or
[`json_pointer::operator!=`](../api/json_pointer/operator_ne.md).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be confusing.

The member operator== does not replace the non-member operators due to deprecation but due to the used C++ standard version.

Until C++20 the non-member operators operator==(json_pointer, string_t) and operator==(string_t, json_pointer) are deprecated for operator==(json_pointer, json_pointer). (Same applies to operator!=.)

Since C++20 the member operator json_pointer::operator==(string_t) is deprecated for json_pointer::operator==(json_pointer). json_pointer::operator!= does not exist in the source code but is synthesized by the compiler by rewriting the expression using json_pointer::operator==, i.e., a == b is rewritten as !(a == b).

This is by no means necessary but could reduce code size minimally. (Someone – me? – should verify that on Compiler Explorer.)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also confused. Can you propose a better wording for the paragraph?

nlohmann marked this conversation as resolved.
Show resolved Hide resolved

=== "Deprecated"

```cpp
nlohmann::json::json_pointer lhs("/foo/bar/1");
assert(lhs == "/foo/bar/1");
```

=== "Future-proof"

```cpp
nlohmann::json::json_pointer lhs("/foo/bar/1");
assert(lhs == nlohmann::json::json_pointer("/foo/bar/1"));
```

- The implicit conversion from JSON Pointers to string
([`json_pointer::operator string_t`](../api/json_pointer/operator_string_t.md)) is deprecated since 3.11.0. Use
[`json_pointer::to_string`](../api/json_pointer/to_string.md) instead.

=== "Deprecated"

```cpp
nlohmann::json::json_pointer ptr("/foo/bar/1");
std::string s = ptr;
```

=== "Future-proof"

```cpp
nlohmann::json::json_pointer ptr("/foo/bar/1");
std::string s = ptr.to_string();
```

- Passing a `basic_json` specialization as template parameter `RefStringType` to
[`json_pointer`](../api/json_pointer/index.md) is deprecated since 3.11.0. The string type can now be directly
provided.

=== "Deprecated"

```cpp
using my_json = nlohmann::basic_json<std::map, std::vector, my_string_type>;
nlohmann::json_pointer<my_json> ptr("/foo/bar/1");
```

=== "Future-proof"

```cpp
nlohmann::json_pointer<my_string_type> ptr("/foo/bar/1");
```
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

#### Miscellaneous functions

- The function `iterator_wrapper` is deprecated since 3.1.0. Please use the member function
[`items`](../api/basic_json/items.md) instead.

=== "Deprecated"

```cpp
for (auto &x : nlohmann::json::iterator_wrapper(j))
{
std::cout << x.key() << ":" << x.value() << std::endl;
}
```

=== "Future-proof"

```cpp
for (auto &x : j.items())
{
std::cout << x.key() << ":" << x.value() << std::endl;
}
```

- Function `friend std::ostream& operator>>(const basic_json&, std::ostream&)` is deprecated since 3.0.0. Please use
[`friend operator<<(std::ostream&, const basic_json&)`](../api/operator_ltlt.md) instead.

=== "Deprecated"

```cpp
j >> std::cout;
```

=== "Future-proof"

```cpp
std::cout << j;
```

- The legacy comparison behavior for discarded values is deprecated since 3.11.0. It is already disabled by default and
can still be enabled by defining
[`JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`](../api/macros/json_use_legacy_discarded_value_comparison.md).

=== "Deprecated"

```cpp
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
#include <nlohmann/json.hpp>
```

=== "Future-proof"

```cpp
#include <nlohmann/json.hpp>
```

## Replace implicit conversions

Implicit conversions via [`operator ValueType`](../api/basic_json/operator_ValueType.md) will be switched off by default
in the next major release of the library.

=== "Deprecated"

```cpp
nlohmann::json j = "Hello, world!";
std::string s = j;
```

=== "Future-proof"

```cpp
nlohmann::json j = "Hello, world!";
auto s = j.get<std::string>();
```
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

You can prepare existing code by already defining
[`JSON_USE_IMPLICIT_CONVERSIONS`](../api/macros/json_use_implicit_conversions.md) to `0` and replace any implicit
conversions with calls to [`get`](../api/basic_json/get.md).
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

## Import namespace `literals` for UDLs

The user-defined string literals [`operator""_json`](../api/operator_literal_json.md) and
[`operator""_json_pointer`](../api/operator_literal_json_pointer.md) will be removed from the global namespace in the
next major release of the library.

=== "Deprecated"

```cpp
nlohmann::json j = "[1,2,3]"_json;
```

=== "Future-proof"

```cpp
using namespace nlohmann::literals;
nlohmann::json j = "[1,2,3]"_json;
```

To prepare existing code, define [`JSON_USE_GLOBAL_UDLS`](../api/macros/json_use_global_udls.md) to `0` and bring the
string literals into scope where needed.

## Do not hard-code the complete library namespace

The [`nlohmann` namespace](../features/namespace.md) contains two sub-namespaces to avoid problems when different
versions or configurations of the library are used in the same project. Always use `nlohmann` as namespace or, when the
exact version and configuration is relevant, use macro
[`NLOHMANN_JSON_NAMESPACE`](../api/macros/nlohmann_json_namespace.md) to denote the namespace.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section is tricky. I keep oscillating on what is necessary and when.

I'll expand on this later.

(Also, "two sub-namespaces" is definitely wrong now.)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the "two sub-namespaces". No idea about the rest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hesitate because of the advice given here:
https://json.nlohmann.me/features/arbitrary_types/#how-do-i-convert-third-party-types

Currently, I think this section is OK (i.e., the issue is resolved for the purpose of this PR) but I'm conflicted about the advice to use NLOHMAN_JSON_NAMESPACE_BEGIN when specializing adl_serializer.


=== "Dangerous"

```cpp
void to_json(nlohmann::json_abi_v3_11_2::json& j, const person& p)
{
j["age"] = p.age;
}
```

=== "Future-proof"

```cpp
void to_json(nlohmann::json& j, const person& p)
{
j["age"] = p.age;
}
```

=== "Future-proof (alternative)"

```cpp
void to_json(NLOHMANN_JSON_NAMESPACE::json& j, const person& p)
{
j["age"] = p.age;
}
```

## Do not use the `details` namespace

The `details` namespace is not part of the public API of the library and can change in any version without announcment.
Do not rely on any function or type in the `details` namespace.
5 changes: 4 additions & 1 deletion docs/mkdocs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ theme:
- navigation.tabs
- navigation.indexes
- navigation.top
- content.tabs.link

nav:
- Home:
Expand Down Expand Up @@ -87,6 +88,7 @@ nav:
- features/types/number_handling.md
- Integration:
- integration/index.md
- integration/migration_guide.md
- integration/cmake.md
- integration/package_managers.md
- integration/pkg-config.md
Expand Down Expand Up @@ -332,7 +334,8 @@ markdown_extensions:
- pymdownx.superfences
- pymdownx.tasklist:
custom_checkbox: true
- pymdownx.tabbed
- pymdownx.tabbed:
alternate_style: true
- pymdownx.tilde
- pymdownx.snippets:
base_path: docs
Expand Down