Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write json to file as text Elixir

Tags:

json

elixir

I have done the following is there a better way to do this?:

File.write("../hello", Poison.encode!(some_stuff))
like image 283
jmunsch Avatar asked Jan 27 '16 15:01

jmunsch


People also ask

How to convert JSON to TXT?

Click on the ‘ Import JSON File ‘ button: Locate your JSON file. In our case, locate the Product_List file, and then click on Open: Press on the ‘ Convert JSON to TXT ‘ button:

How to write data to a file using JSON?

Write a data in file using JSON use json.dump () or json.dumps () used. write like this to store data in file. this example in list is store to a file. This does the same thing as @phihag's answer, but is not guaranteed to work at all times. Consider such code: f = open ('1.txt', 'w'); f.write ('a'); input ().

What license does elixir JSON library come under?

The Elixir JSON library is available under the BSD 3-Clause aka "BSD New" license Built using ExDoc(v0.23.0) for the Elixir programming language. Designed by Friedel Ziegelmayer.

How to add JSON encoder and decoder in Elixir?

Elixir JSON This library provides a natively implemented JSON encoder and decoder for Elixir. You can find the package in hex.pmand the documentation in hexdocs.pm. All contributions are welcome! Installing Simply add {:json, "~> 1.4"}to your project's mix.exsand run mix deps.get. Usage Encoding an Elixir type


1 Answers

You are missing the third argument, the mode options. Something like this would work:

File.write("../hello", Poison.encode!(some_stuff), [:binary])

Other than the missing argument, what you have is a line of code that easy to understand. With your use of encode! an exception will be raised if the encoding fails. If you want to ensure the file is written successfully without checking the return value from File.write/3 you should use File.write!/3. Using File.write!/3 fits better with Erlang's "let it crash" philosophy (http://learnyousomeerlang.com/errors-and-exceptions). Though it all depends on the context of this line...

like image 76
Stratus3D Avatar answered Nov 14 '22 22:11

Stratus3D