I would like to find a way to use phpdoc2 to parse PHP projects into JSON rather than into Abstract Syntax Tree XML. Although of course I could just parse the XML into JSON, it seems that creating XML would be unnecessary overhead here.
So, in a nut shell, the question is: is there an easy way to configure phpdoc2's parser to directly produce JSON instead of XML? Or maybe some clues on what to extend in phpdoc2 to route the parsing output into JSON?
The story behind this question is: I would like to create JSDuck-like documentation for my PHP project. Although I have found that JSDuck can be used with PHP projects I won't go that way for the two reasons:
Don't want to part with phpdoc comments in my PHP classes or add something JSDuck-specific in there;
Don't really need the whole JSDuck doc interface as I am going to create a very custom one myself;
Prefer a PHP solution.
After half a day spent on phpdoc2 I finally found a solution which I believe is the right one. One should not worry or even know about Abstract Syntax Tree XML in phpdoc2 to achieve the goal.
The solution is:
Create a new writer class Json.php and place it in src/phpDocumentor/Plugin/Core/Transformer/Writer/ along with other writers. A good start point is to take the Graph.php writer and rewrite it to output JSON instead of SVG;
Create and use a new simple template like:
<?xml version="1.0" encoding="utf-8"?>
<template>
<transformations>
<transformation writer="Json" artifact="classes.json" />
</transformations>
</template>
Add Json writer to src/phpDocumentor/Plugin/Core/ServiceProvider.php's register method:
$writerCollection['Json'] = new Writer\Json();
And finally, just use the template when calling phpdoc on your project.
Related
Sorry for the inappropriate question. But what do you recommend me to use to structure a library that can put a query arrangement on json formats generated by an XML parsing based on TEI p5? I tried to use GraphQL by converting the interfaces of my Angular application related to parsing information from XML to JSON in type to define a GraphQL schema but I don't think that's the way.
What I have to do is query, client only, some data encoded in XML (also wanting already parsed in JSON) and, for example, search for all occurrences of a specific data.
Do you have any roadmaps to recommend or some JSON query system that might be right for me?
You might take a look at https://www.npmjs.com/package/saxon-js. With SaxonJS you're able to run XPath expression against XML using JavaScript.
Team,
This has been a problem in apps-script development for me. I have been trying to solve it for quite some time.
The XML appscript service I have been using (now deprecated) has been doing excellent, I mean really awesome JOB of parsing the any html file or XML file with the linient flag turned of. I am in love with it.
Also, this service gives you a JSON view of XML file. that is really cool. because I can simply browse to any data item in the debugger, and then type that in code. No need to call many sensless methods when you know exactly what your code is looking for.
These AWESOME features in deprecated XML service make the job a lot easier than the so called XMLService which it replaces.
One issue, I did find with XML service is it is slow. I wrote a parser that does it within 10 seconds that XML Service takes 3 minutes. So, sure it is bad.
But, XMLService does not have those neat features.
IS there any public code or libraries by anyone that does a beautiful job of:
a. Converting XML to JSON view.
b. Parse HTML in linient form.
c. Do it fast
Please help.
Ok, I didn't receive any update on the matter. So, I have written it myself. I simply convert XML to JSON.
Xml2Json will represent an XML string as a JSON object.
If you set flags="attributes", then the element attributes are packaged in XmlAttributes child of the JSONobject.
In XML, there is no way to say, you are about to encounter an array of elements or a single element. So, the parser by default creates a JSON object out of an element unless it counters another xml element with same name, then it converts the Object into an Array.
Here is the Xml2Json Project Key: 18Aji4ggm4A2cXQ1n_sSc6gDQA3Wc4aC5WvQYybAflkdUushu9f2Ogv5o
I am new to JSP and I am having trouble finding a simple concrete example to make a request to a server that returns a JSON object.
What I am trying to do is something like:
myjson_object = getJSONfrom("my.webserver.com/get/json")
I basically want to add such a line to an existing JSP page so that I can have the JSON object available. I would like to avoid using AJAX or JQuery. I have found several examples like this one, but for some reason they do not work in my case.
Probably trying to do that operation into JSP is not a good idea, since you'd need to use scriptlets and I understand scriptlets are now considered not a good practice (see this)... You'd better do it into a servlet (see this)...
Anyway the code would be similar. You first need to make the request and then parse the JSON response...
In order to make the request you can use HttpURLConnection class. See this question.
Once you have the server response into a BufferedReader, you can parse the JSON using some library, such as Gson. You can find lots of examples of JSON deserializing using Gson in SO, like this or this.
I'm trying to return an html snippet from a service that can only return valid JSON.
I've tried some things like:
This gets me a bunch of character like \n\n\n\n\t\t\t\t
return JSON.stringify({html: $('body').html()});
or
return JSON.stringify($('body').html());
On the receiving end, I'd like to be able to parse that HTML via Cheerio, or jQuery or JSDom so I can then run queries like $(".some_selector") on that data.
What is the proper way of doing this? Any special libraries / methods that can handle the escaping for me? I've googled it, but haven't had any clear results...
Thanks.
On the receiving end, you need to simply undo the JSON serialization. That's it!
Your HTML will be in its regular format at obj.html, which you can then parse with whatever DOM parser you want.
Well, you are probably going to need to worry about quotes in the HTML (like with attributes) because the could interfere with the quotes that delimit your JSON values.
Here is similar question as well as this web page that explains some of what you need to consider.
Briefly looking at npmsj.org, I didn't see any reputable modules that might help you make HTML JSON compatible, but I think you can probably figure it out fairly easily on your own, given a large enough sample set of HTML. You can always run your JSON through this validator to check it. I suppose you could also simply do a JSON.parse(jsonContainingHtml) on it as well. You'll get an exception if the string is not valid JSON.
IIs there a package similar to HTml::Template in perl which takes a JSON object and maps it to a HTML template file? I am building a web application using HTML::Template and will be receiving JSOn from a web services API, things will be made simple if I can templatize this JSOn to HTML instead of doing it the exact way HTML::Template requires.
HTML::Template just takes a data structure consisting of strings, hashes and arrays. JSON maps directly onto that.
$template->param(myData => JSON::Any->new->decode($json_string));
HTML::Template is a rather 'simple' templating engine - I am using quotes because its simplicity let's you do whatever you need in a view part from the Model View Controller architecture.
However, you can not execute arbitrary perl code inside a HTML::Template.
Also, due to the fact that in JSON you could have very complex data structures, I doubt that there are any suitable ways of using JSON in a straight way in your templates.
The only solution I see as possible is for you to use a Perl script that will parse the JSON, create some 'objects' and pass them to your templates. You already have that perl script - is the one that instantiate your HTML::Template object.
ok, a bit late, but:
HTML::Template always wants a hash of arrays of hashes and so on.
and you cannot navigate through the parameter stash.
If you want to do this, you might try out HTML::Template::Compiled which allows you to do this.
<tmpl_var some_hash.key.another_key[23] >
or with alternative delimiters:
[%= some_hash.key.another_key[23] %]
but note the documented differences of the module to HTML::Template.
So you decode your JSON string to a data structure and pass it to the template and then you can access all values somewhere deep in the structure.