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

Make json_writer a structured_writer, improve output formatting #3227

Merged
merged 4 commits into from
Sep 6, 2023

Conversation

WardBrian
Copy link
Member

Submission Checklist

  • Run unit tests: ./runTests.py src/test/unit
  • Run cpplint: make cpplint
  • Declare copyright holder and open-source license: see below

Summary

Follow up on #3191.

This PR:

  • Improves the output of the JSON writer to be a bit more human readable (mainly by more consistently outputting newlines between key:value pairs)
  • Updates both structured_writer and json_writer such that the latter can be a subclass of the former.
  • Fixed a bug where j.begin_record("foo"); j.end_record(); j.write("anything here"); would generate invalid JSON

Intended Effect

How to Verify

Side Effects

Documentation

Copyright and Licensing

Please list the copyright holder for the work you are submitting (this will be you or your assignee, such as a university or company):

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the following licenses:

Copy link
Collaborator

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

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

Few small changes

Comment on lines 104 to 111
template <typename T>
void write_int_like(const std::string& key, T value) {
if (output_ == nullptr)
return;
write_sep();
write_key(key);
*output_ << value;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would just call this write_ since it takes anything that is valid for operator<<

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but I want to make it clear that things like double (which are valid for operator<<) should not be passed to this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh for NA etc.

I'd still call it write_ and have the signature like

template <typename T, stan::require_integral_t<T>* = nullptr>
  void write_int_like(const std::string& key, T value) 

Comment on lines 351 to 352
if (output_ == nullptr)
return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (output_ == nullptr)
return;
if (output_ == nullptr) {
return;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Done for all the functions

Comment on lines +402 to +410
*output_ << "[ ";
if (v.size() > 0) {
auto last = v.end();
--last;
for (auto it = v.begin(); it != last; ++it) {
*output_ << *it << ", ";
}
}
*output_ << v.back() << " ]";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same code is replicated across multiple functions. Is there a reason you got rid of write_vector?

Copy link
Member Author

Choose a reason for hiding this comment

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

The value type is printed differently in each case - this one uses <<, the vector uses process_string(), etc.

Write_vector was also just four functions which were the same as these but called from a template. To use this as a derived class we needed un-templated versions

Comment on lines +418 to +419
void write(const std::string& key,
const std::vector<std::complex<double>>& v) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need to make these virtual in structured_writer?

Copy link
Collaborator

@SteveBronder SteveBronder left a comment

Choose a reason for hiding this comment

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

Looks good besides the int thing which I'll leave as optional

@WardBrian WardBrian merged commit 32084ad into develop Sep 6, 2023
@WardBrian WardBrian deleted the feature/json-prettify branch September 6, 2023 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants