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

JSON Class Hierarchy #2936

Merged
merged 2 commits into from
Jul 6, 2016
Merged

Conversation

bmulvihill
Copy link
Contributor

Based on #1387
Allows a user to pass in -f parameter when using the hierarchy tool and obtain JSON class hierarchy

@bmulvihill bmulvihill changed the title json hierarchy JSON Class hierarchy Jul 1, 2016
@bmulvihill bmulvihill changed the title JSON Class hierarchy JSON Class Hierarchy Jul 1, 2016
case format
when "json"
JSONHierarchyPrinter.new(program, exp).execute
else
Copy link
Member

Choose a reason for hiding this comment

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

The else branch should rather abort and tell the user about the invalid parameter argument.

@asterite
Copy link
Member

asterite commented Jul 1, 2016

Overall looks good. A nitpick of mine would be to avoid generating these intermediate hash and directly stream the JSON to the output. Check the example here, you basically want to pass an IO to the printer, STDOUT, and then append to it as you traverse the types, using io.json_object and json.field.

And finally, run crystal tool format on the code, or use Sublime Text to edit the code.

@bmulvihill
Copy link
Contributor Author

@asterite @jhass Thanks for taking a look! I'll make some adjustments.

@asterite I was originally trying to stream the JSON to the output via the json_object helper, but was having some issues, I'll take another crack at it.

@bmulvihill bmulvihill force-pushed the json-hierarchy branch 4 times, most recently from aaee042 to f7c3ee1 Compare July 4, 2016 01:06
@bmulvihill
Copy link
Contributor Author

@asterite I made some changes and am now directly streaming to JSON to the output. I was attempting to push the json_objects into json_arrays, but ran into some issues, instead I am now manually creating the arrays within the io stream. Let me know if this is acceptable, thanks!

compute_targets(@program.types, exp, false)
end

json = String.build do |io|
Copy link
Member

Choose a reason for hiding this comment

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

From here, to the end of the method, this should be:

      STDOUT.json_object do |json_object|
        print_type(@program.object, json_object, STDOUT)
      end

otherwise you are streaming to a string in memory, thus creating an object, which is what we wanted to avoid in the first place.

That won't compile because of the type restrictions bellow (JSON::ObjectBuilder(String::Builder)). Just remove those restrictions, they aren't necessary here. Then it will compile.

@asterite
Copy link
Member

asterite commented Jul 4, 2016

@bmulvihill Looks really good, thanks!

It's true, it's a bit hard to use json_array here, I'll take this as an opportunity to think about how to improve that :-)

I've made a few more comments, these should be the last ones.

@bmulvihill
Copy link
Contributor Author

bmulvihill commented Jul 5, 2016

@asterite I made more changes. I had to do STDOUT.flush after streaming, otherwise I was getting EOF errors. Is this expected, or was I missing something?

@asterite
Copy link
Member

asterite commented Jul 5, 2016

@bmulvihill Thanks, I'll soon take a look. In my tests I didn't need to flush, I'll probably merge your work and fix anything that I find, if I find anything :-)

@asterite
Copy link
Member

asterite commented Jul 6, 2016

@bmulvihill Let's do the flush, the error you get is #2961

@asterite asterite merged commit 0120145 into crystal-lang:master Jul 6, 2016
@asterite asterite added this to the 0.19.0 milestone Jul 22, 2016
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.

None yet

3 participants