Skip to content

Conversation

@jini-zh
Copy link
Contributor

@jini-zh jini-zh commented Apr 30, 2025

Provide json_encode for

  • char*
  • generic pointers (C arrays)
  • std::array

Implement json_encode_object --- a helper function to construct JSON objects out of variables and literals.

There is a subtlety when it comes to extending json_encode for user-defined objects that I ran into while working on BUTTON high voltage control implementation, see 8361521. It needs further research. Please keep the commit message for the future.

Evgenii Zhemchugov added 4 commits April 30, 2025 11:55
Testing revealed difficulties regarding extending json_encode with
user-defined types. In the following chain of calls:

    struct S { ... };
    json_encode(std::string,  std::vector<std::array<S, 6>>); // 1
    json_encode(std::ostream, std::vector<std::array<S, 6>>); // 2
    json_encode(std::ostream, std::array<S, 6>*, ...); // 3
    json_encode(std::ostream, std::array<S, 6>); // 4
    json_encode(std::ostream, S*); // 5
    json_encode(std::ostream, S); // 6, implemented in the user code

the compiler might only be able to find the user function if it was
called with unqualified names and argument-dependent lookup took
action. Also, without forward declarations provided by this patch the
compiler could not reach step 4 since json_encode for array has been
declared after json_encode for pointers. This suggests possible problems
with user and other STL containers. See

https://stackoverflow.com/questions/28187170/c-enforce-second-pass-name-lookup-in-template-function
https://akrzemi1.wordpress.com/2015/11/19/overload-resolution/

for the discussion.

One possible solution is to declare

    template <typename T> void json_encode(std::ostream&, const T&);

and use template specializations instead of overloaded functions, but it
needs special casing to exclude arithmetic types implemented by a
different template --- and possibly it may clash in a similar way with
other user defined types.
@brichards64 brichards64 merged commit 5bb7112 into ToolFramework:main May 8, 2025
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants