How to Write JSON in Python
Python's json module provides dumps() function to convert objects to JSON strings, using the same module as for parsing JSON.
Posts tagged with "polyglotbits"
Python's json module provides dumps() function to convert objects to JSON strings, using the same module as for parsing JSON.
Python makes reading files simple with built-in context managers that automatically handle file closing, similar to Ruby's block approach.
Csharp was probably the first compiled language I really loved.
Reading a file in lua is pretty straight forward.
Lua is among the programming languages that I write about here I have used the least.
Reading a file in C++ is almost identical to how it's done C.
To parse json in Rust you need a library - the one used here is called rustc-serialize.
In C++ there are no built-in ways to handle json, however there are many open source libraries to help you.
The go standard library has all you json needs covered - the JSON package has methods for both encoding and decoding to/from JSON.
Writing a file in Javascript is as easy as using the built-in method fs.writeFile.
The Euler 1 problem is fairly simple to solve - most people intuitively know how to do it.
The simplest way of writing to a file in php is using the file_put_contents function.
In haskell there are two ways to read the stdin stream - which one to use depends on what you're trying to accomplish.
Reading from stdin in C++ is surprisingly concise - only 12 lines of code including includes and empty lines.
Writing to a file in c is surprisingly simple - you open a file, write to it and close it again.
Reading a file in Javascript (nodejs) is as easy as using the built-in method fs.readFile.
In go you will sometimes write more code than you are used to in most higher level languages, but will have a lot more control over how it is done.
Just as with reading program arguments, lisp looks a bit weird when reading from the stdin stream - until you get the hang of it.
Lisp is a completely different way of looking at programming - where in Ruby everything is an object, in lisp everything is a list.
Unlike Javascript and PHP you don't call a function with one parameter - instead you can call to_json on the object you want to convert.
In Ruby you parse json by first including the json library provided with the language.
In PHP JSON stringify and parse are part of the core libraries - however in PHP they are called json_encode and json_decode respectively.
JSON is an integral part of the JavaScript language, so it should come as no surprise that parsing json is equally easy.
Reading from stdin in java looks like in most languages - Standard in is bound to System.in and must be opened as if it where a file.
Reading from stdin in ruby is as easy as calling STDIN.read
In ruby you have lowlevel access to all the same methods that you do in C, but you also have some higher level sugar on top.
In c you are as close to the metal as possible in a high-level language, which shows in the verbose code you need to make a simple tcp server.
If c is the most verbose way to write a tcp server, Javascript (nodejs) is probably the least verbose way.
In javascript (nodejs anyway) stdin can be read as a stream of bytes, so you need to tell node which encoding it is.
Reading from stdin in PHP is just like reading a file - the only difference is that the file to read is called php://stdin
Reading files in elixir is pretty straight forward - especially if you just want the content of a file you know exists.
In Ruby there is no built-in support for parsing and writing xml, but my favourite tool for working with xml in ruby is nokogiri.
I don't know much about PowerShell to be honest - it sounds to me like some kind of a windows version of bash/zsh.
Reading perl has always been weird for me, but you can see where PHP and Ruby have borrowed many of their ideas.
In C# program arguments are a part of the main method signature, like in most c-based languages.
Elixir is rather easy - as long as you get past the pattern matching barrier I stumpled into when writing about reading program arguments.
Since json is short for JavaScript Object Notation, it does not come as a surprise that is a natural part of the javascript language.
Once again php shows how it is an easy language for beginners - you don't have to think about much when you want to parse json.
Reading a whole file is as easy as one function call in PHP.
Reading from stdin in go is as easy as reading from os.Stdin - the ioutil.ReadAll method reads all bytes into a buffer.
Reading from stdin in c is exactly like reading from a file - in unix philosophy everything is a file after all.
In ruby you read a file in two steps - first open the file, and then iterate over the contents.
Reading files in C is very easy - you need stdio.h, a FILE pointer, and call fopen on a filename.
Rust reminds me of C in many ways - the program arguments include the program name at the first index.
This is the one programming language to date where I have had the hardest time doing the little hello world program.
Go has a main method with no arguments in its signature - you read the program arguments from os.Args.
Elixir is fundamentally different than many programming languages, which also shows in the simple task of making the Hello World program.
In JavaScript (nodejs) you can access the program arguments in the array process.argv, but you have to skip two entries to get to the real arguments.
In Java like in C the program options are part of the main method signature.
Ruby has a global ARGV array which contains all arguments, but unlike C and PHP it does not contain the script name.
In C program arguments are part of the main method signature, which means you can read a specific argument by reading the nth index of the argv array.
In PHP the program arguments of current script is always available in the $argv variable.