Skip to content

Commit c1aaf41

Browse files
committed
Allow json printing to be in different formats
Either human readable with each variable on a new line, or harder to read with each node all on the same line.
1 parent 097f940 commit c1aaf41

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

include/inch/nuclide.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ class Nuclide
584584
*
585585
* \return All of the members in the format of a json ... unit
586586
*/
587-
[[nodiscard]] std::string writeAsJSON() const;
587+
[[nodiscard]] std::string writeAsJSON(const bool human_readable = true) const;
588588

589589
/**
590590
* Function to calculate the error on a value via sum of squares

src/nuclide.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,33 @@ std::string Nuclide::writeAsCSV(std::string_view sep) const
604604
}
605605

606606

607-
std::string Nuclide::writeAsJSON() const
607+
std::string Nuclide::writeAsJSON(const bool human_readable) const
608608
{
609-
return fmt::format("{{\"A\":{},\"Z\":{},\"N\":{},\"Symbol\":\"{}\",\"Decay\":\"{}\",\"Experimental\":{},\"Own\":{},"
610-
"\"NubaseMassExcess\":{},\"ErrorNubaseMassExcess\":{},\"AMEMassExcess\":{},\"ErrorAMEMassExcess\":"
611-
"{},\"HalfLife\":{},\"SingleNeutronSeparationEnergy\":{},\"ErrorSingleNeutronSeparationEnergy\":{}"
612-
",\"SingleProtonSeparationEnergy\":{},\"ErrorSingleProtonSeparationEnergy\":{},"
613-
"\"DoubleNeutronSeparationEnergy\":{},\"ErrorDoubleNeutronSeparationEnergy\":{},"
614-
"\"DoubleProtonSeparationEnergy\":{},\"ErrorDoubleProtonSeparationEnergy\":{},\"Year\":{}}}",
609+
// Data files go to 4dp so we'll print the same
610+
return fmt::format("{{{0}"
611+
"\"A\":{1},{0}"
612+
"\"Z\":{2},{0}"
613+
"\"N\":{3},{0}"
614+
"\"Symbol\":\"{4}\",{0}"
615+
"\"Decay\":\"{5}\",{0}"
616+
"\"Experimental\":{6},{0}"
617+
"\"Own\":{7},{0}"
618+
"\"NubaseMassExcess\":{8:.4f},{0}"
619+
"\"ErrorNubaseMassExcess\":{9:.4f},{0}"
620+
"\"AMEMassExcess\":{10:.4f},{0}"
621+
"\"ErrorAMEMassExcess\":{11:.4f},{0}"
622+
"\"HalfLife\":{12:.3e},{0}"
623+
"\"SingleNeutronSeparationEnergy\":{13:.4f},{0}"
624+
"\"ErrorSingleNeutronSeparationEnergy\":{14:.4f},{0}"
625+
"\"SingleProtonSeparationEnergy\":{15:.4f},{0}"
626+
"\"ErrorSingleProtonSeparationEnergy\":{16:.4f},{0}"
627+
"\"DoubleNeutronSeparationEnergy\":{17:.4f},{0}"
628+
"\"ErrorDoubleNeutronSeparationEnergy\":{18:.4f},{0}"
629+
"\"DoubleProtonSeparationEnergy\":{19:.4f},{0}"
630+
"\"ErrorDoubleProtonSeparationEnergy\":{20:.4f},{0}"
631+
"\"Year\":{21}{0}"
632+
"}}",
633+
human_readable ? "\n" : "",
615634
A,
616635
Z,
617636
N,

0 commit comments

Comments
 (0)