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

Update JSON package to version 0.4 #3670

Open
wants to merge 8 commits into
base: development
Choose a base branch
from

Conversation

d-torrance
Copy link
Member

@d-torrance d-torrance commented Feb 23, 2025

We export remove the JSONEncoder class so that developers can write their own toJSON methods. Thanks @mahrud for the suggestion!

We can deal with everything by just using options to the toJSON
method.  In particular, we add an IndentLevel option which keeps track
of the current indentation level, which was the primary purpose of the
old JSONEncoder class.

We also change some of the internal methods to functions.  There was
no multiple dispatch, so using methods was unnecessary.
@mahrud
Copy link
Member

mahrud commented Feb 23, 2025

Also, take a look at various json methods I had to define to deal with serializing hypertext objects and documentation nodes here. I had to resort to a hacky middle ground which you might not like, namely having toJSON VisibleList return an Array and toJSON HashTable return a HashTable, then writing format' to convert to a string. This was necessary to handle nested hash tables and lists without superfluous quotations all over the place, but perhaps there is an easier solution that I missed.

I don't remember the reason for each particular entry, but once you have toJSON Hypertext, as a challenge try something like this with different packages:

elapsedTime pkg = loadPackage(pkgname, Reload => true, LoadDocumentation => true)

L = new HashTable from pkg#"raw documentation";
L = selectValues(L, x -> not x#?PrimaryTag and not x#?"undocumented");
elapsedTime ("static/"|pkgname|".json") << toJSON(L, Indent => 2) << flush << close

I still can't get Macaulay2Doc serialized this way, but smaller packages work fine.

@d-torrance
Copy link
Member Author

Cool, I'll check it out!

@mahrud
Copy link
Member

mahrud commented Feb 24, 2025

This is not valid JSON:

i14 : json { "A\nB" }

o14 = ["A
      B"]

i15 : fromJSON json { "A\nB" }
stdio:16:8:(3): error: syntax error at token '
', position 3

@d-torrance
Copy link
Member Author

I added a few new toJSON methods so that calling toJSON on a package's raw documentation appears to mostly work.

@d-torrance
Copy link
Member Author

This is not valid JSON:

Indeed! Good catch.

@mahrud
Copy link
Member

mahrud commented Feb 24, 2025

I added a few new toJSON methods so that calling toJSON on a package's raw documentation appears to mostly work.

Now try fromJSON and compare with the raw documentation haha

@mahrud
Copy link
Member

mahrud commented Feb 24, 2025

I think other than the newline issue this is working great.

An annoying source of complexity for me was if I want json TO to print links in a different way, then I also need to reimplement json SPAN and any other container which may contain a TO. The shortcut is to just redefine html TO, but that would have other side-effects that break the rest of the system. I'm not sure how to handle this best.

@mahrud
Copy link
Member

mahrud commented Feb 24, 2025

This is mostly unrelated, but do you know what's happening here?

i35 : html Saturation#"raw documentation"#"annihilator"#Inputs#0

o35 = <span><span class="tt">M</span>, $\left\{\texttt{an }\texttt{DocumentTag}\left\{\mathit{Format}\ \Rightarrow \
      \texttt{Ideal},\:\mathit{Key}\ \Rightarrow \ \mathit{Ideal},\:\mathit{Package}\ \Rightarrow \
      \texttt{Macaulay2Doc},\:\texttt{RawDocumentation}\ \Rightarrow \ \texttt{null}\right\}\texttt{ideal},\:\texttt{,
      },\:\texttt{a }\texttt{DocumentTag}\left\{\mathit{Format}\ \Rightarrow \ \texttt{Module},\:\mathit{Key}\ \Rightarrow \
      \mathit{Module},\:\mathit{Package}\ \Rightarrow \ \texttt{Macaulay2Doc},\:\texttt{RawDocumentation}\ \Rightarrow \
      \texttt{null}\right\}\texttt{module},\:\texttt{, or },\:\texttt{a }\texttt{DocumentTag}\left\{\mathit{Format}\
      \Rightarrow \ \texttt{RingElement},\:\mathit{Key}\ \Rightarrow \ \mathit{RingElement},\:\mathit{Package}\ \Rightarrow \
      \texttt{Macaulay2Doc},\:\texttt{RawDocumentation}\ \Rightarrow \ \texttt{null}\right\}\texttt{ring element}\right\}$,
      </span>

@d-torrance
Copy link
Member Author

Maybe this is overkill, but I went ahead and updated format so that it escapes newline characters (plus a few others -- all the ones we can escape in M2 strings). This seems like reasonable behavior based on its documentation:

a string obtained from s by inserting escape sequences, thereby preparing it for printing in a form suitable for reading in again

It's still not quite right when composing with fromJSON, though:

i2 : fromJSON toJSON "foo\nbar"

o2 = foo\nbar

i3 : oo == "foo\nbar"

o3 = false

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