Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- Be respectful, on topic and if you see a problem, Flag it.
If you would like to contact our Community Manager personally, feel free to send a private message or an email.
Pretty print
billy2
Member, OS Professional, Mentor, Developers, User Group Leader Posts: 2,068 PRO
I'm trying to write a simple feature script and got caught up on pretty printing maps. I wrote a simple function that parses the returned data and formats it into a readable structure. Below is a listing of the pretty print function.
Below is displaying 2 simple maps. The 1st is a standard string based representation of the map. The 2nd is the same map except it's pretty printed showing the structure of the map.
Below is a more complex map showing both the simple & the pretty version of the map.
The problem comes when you're trying to access something inside the map. Without pretty print, it's hard to create an accessor to a value contained inside the map.
JSON has the same issues when working with an API. Javascript has many string -> object and object -> string operators and it's easy to create pretty print so you can understand an objects structure.
There may be better ways to understand the structure from a response in the FS console, if there is a better pattern, please let me know.
</code>//pretty print</pre><pre class="CodeBlock"><code> export function p(s) { var str = splitIntoCharacters(toString(s)); var i = 0; //indent var c; //cnt var t = " "; //tab // println(size(str)); for (c = 0; c < size(str); c += 1) { if (str[c] == "{") { print(str[c]); print("\n"); i += 1; for (var a = 0; a < i; a += 1) print(t); } if (str[c] == "[") { print("\n"); for (var a = 0; a < i; a += 1) print(t); print(str[c]); print("\n"); i += 1; for (var a = 0; a < i; a += 1) print(t); } if (str[c] == "}") { i -= 1; print("\n"); for (var a = 0; a < i; a += 1) print(t); print(str[c]); } if (str[c] == "]") { i -= 1; print("\n"); for (var a = 0; a < i; a += 1) print(t); print(str[c]); } if (str[c] == ",") { print(str[c]); print("\n"); for (var a = 0; a < i; a += 1) print(t); } if (str[c] != "{" && str[c] != "}" && str[c] != "[" && str[c] != "]" && str[c] != "," && str[c] != " ") print(str[c]); } println(); }
Below is displaying 2 simple maps. The 1st is a standard string based representation of the map. The 2nd is the same map except it's pretty printed showing the structure of the map.
Below is a more complex map showing both the simple & the pretty version of the map.
The problem comes when you're trying to access something inside the map. Without pretty print, it's hard to create an accessor to a value contained inside the map.
JSON has the same issues when working with an API. Javascript has many string -> object and object -> string operators and it's easy to create pretty print so you can understand an objects structure.
There may be better ways to understand the structure from a response in the FS console, if there is a better pattern, please let me know.
5
Comments
Legit! I will use this for sure.
Learn more about the Gospel of Christ ( Here )
CADSharp - We make custom features and integrated Onshape apps! Learn How to FeatureScript Here 🔴
https://thesmoothcut.com/
This would have helped and will keep it in mind if I find myself messing around with the API for sure!
(If this makes it into the FS standard library, it will probably end up as a function in string.fs, same as println.)