Persistence using Typescript - json

I'm currently working in a project using Electron (basically nodejs with chromium) plus Angular2
Is there any way to save a typescript object in a file?
Currently, I'm saving objects in a json file. The problem is that all object methods are lost
Should I try to save the __proto__ variable?
Should I use a framework or a special database engine?
Today I'm reading the object from a the json file and parsing it using JSON.parse. Then I reassign all the properties in a new object, which is not scalable
Any suggestion is welcome
Thanks in advance. José

I'm the OP!
I just find this library that, apparently, solve the problem
https://www.npmjs.com/package/serializer.ts
So what to do? How to have in users array of User objects instead of
plain javascript objects? Solution is to create new instances of User
object and manually copy all properties to new objects.
Alternatives? Yes, you can use this library. Purpose of this library
is to help you to map you plain javascript objects to the instances of
classes you have created.
I'll give a shot and feedback. Bests

You can export variables as json like:
myJsons.ts:
export var myVar = {
name: "John"
}
and use them inside your ts files like:
myComponent.ts:
import {myVar} from '../path-to-myJsons'
this.myJson = myVar;
this.name = this.myJson.name;

Related

Swift - Store JSON globally

i have this JSON array stored in my local variable:
let bigJsonArray = JSON(response)
my question is if there is any possibility to store this "bigJsonArray" in a global variable/session/cookie/config so i can access it in every view of my app ?
Anybody knows how to process this and could help me?
Greetings and thanks!
What you can do is to define bigJsonArray as a global variable just by defining it outside of any class and the Swift compiler will understand it as a global variable and you can access it from anywhere in your code.
for example:
import UIKit
var bigJsonArray = JSON(response)
class a {
var x = 0
}
that's of curse will not save the data if you killed the app, but from what I understand from your question you just need to be able to access it from all the app without resending a request to the server.
If you want to save the JSON data permanently, you just store the data that you received as a file, and the next time you need it, you read it from the file and parse it (there's actually a method for that) instead of downloading and parsing the data. Much easier than trying to store the parsed data.
If this is data that can be downloaded again, read the appropriate documentation to make sure the file isn't backed up, and is stored in a cache directory where the OS can remove it if space is tight.

AngularJS Domain model object

This is a question for AngularJS developers, what is the best practice for the domain model objects ? File structure ? Class definition ? Modules ?
This will be mainly used for REST web services to translate JSON <-> Domain, is there any tool to convert Java domain classes into Javascript classes ?
I will certainly use $http for the REST api, maybe we can link domain objects directly on a $http service module ?
Thank you !
I am using GSON for convert class to JSON.
It is so easy;
Create a base class like Base.class
Insert add your objects to this class
And convert to JSON like;
DataObject obj = new DataObject();
Gson gson = new Gson();
String json = gson.toJson(obj);
JAR and More Info...
If you can use $resource it will return javascript objects to you. Otherwise $http will return a javascript object representation of the JSON data.
I ended up using normal JS class files with .protoype for methods.
I was wondering if a tool exist converting Java classes into Javascript classes but this is another topic I guess.

Navigate to viewModel and pass Model value

I am getting the data from server in JSON and I am converting that to my viewModal. Now I am loading the a new view using return router.navigate('results'). But at the same time I want to pass the viewModel which I created from the JSON object to be passed to this view. By default the "results" viewModel is getting called but it is having blank values.
A small code snippet will be helpful as I am new to Durandal JS.
The best answer I have is to store that information in a separate module, like so:
storage.js
define(function(require) { return []; });
viewmodel.js
define(['storage'], function(storage) {
$.get('uri', function(data) {
data.forEach(function(obj) {
storage.push(obj);
});
});
});
This is not the most elegant solution. I'm really interested if there is a clean way to pass data from and to separate view models on activation, but so far I have found none.
Ended up finally something like this.
In the module where I want to navigate, I created an "Initialize" function which accepts a JSON object. Using this JSON object I initialized all properties in the viewModel.
From the page from where I have to navigate there I did a "require" on the module where I want to navigate next. Then I called the "Initialize" method and passed my JSON object.
After this I used router.navigate method to go that module.
This way when I navigated, I got all the values which I wanted to pass from one view to other. Hope this approach will help someone else.
The answer from Matthew is certainly similar to the way that I currently do it, once the data is held within your separate module (i have a path called modules/data/someDataStorage) you can use either events or a knockout observable to make the data update through to your view models.
In your case I would make updates to your shared module to store information on your request and then on the activation of your results module, go and get the data from that shared module.

How to convert String values to json object in java without using org.json

I'm trying to convert a JSON String value `{"name:ganesh,sex:male,age:22"} ) into key and value set using json in gwt can anyone help me with some ideas please.
Since you don't want to use org.json, I imagine that you need to convert JSON on the client side. If this is the case, you will need to use GWT's JSON libraries. They could be inherited into your GWT project by adding these lines to your .gwt.xml file:
<inherits name="com.google.gwt.json.JSON" />
<inherits name="com.google.gwt.http.HTTP" />
You'll also need to import the com.google.gwt.json packages into your class files.
The getting started guide to using JSON in GWT can be found here. It also provides examples for decoding JSON.
Your string must be of the form
"{'name':'ganesh','sex':'male','age':'22'}"
or
'{"name":"ganesh","sex":"male","age":"22"}'
You can use one of the following ways ...
Use javascript eval. Embed the eval in JSNI
public static native String eatString(String jstring) /*-{
eval("var hello = " + jstring + ";");
return hello;
}-*/;
Pass the String as script in a JSP GWT hosting file. This way, you can only doing once - when the GWT app is loaded with its hosting file. Your JSP will generate the javascript dynamically for each loading.
Place the following in hour GWT hosting html file, before the script tag where GWT module is called.
<script>
var hello = {'name':'ganesh','sex':'male','age':'22'};
</script>
Then in your GWT app, use the GWT Dictionary class to reference any javascript objects declared in the hosting file.
Use the following utilities, in particular JsonRemoteScriptCall.java to read remote, out of SLD-SOP javascript objects into your GWT app. http://code.google.com/p/synthfuljava/source/browse/trunk/gwt/jsElements/org/synthful/gwt/javascript/#javascript%2Fclient.
Please be warned - out of SLD-SOP objects can be hazardous. Read up on Second level domain, same origin policy browser security.
Use RestyGWT and pretend that the data from the server comforms to REST data structure. But of course, use of json.org and google JSON utils has already been done for you.

Convert loaded string to Object

In AS3, I want lo load a file text with URLLoader. In the file text I have the following string:
{a:1,b:"string",c:["one","two"]}
Is it possible (once loaded) to convert it to an Object?
There is no intrinsic deserializer built into the language, no. But if your text file sticks to the JSON standard, then you could use a JSON parser to do the conversion for you: http://code.google.com/p/as3corelib/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fadobe%2Fserialization%2Fjson
Or, if you cannot adhere to JSON, you could always write your own deserializer.
What you need is to eval the string to create the object.
This is done natively in javascript and AS2. AS3 however does not support this function.
But all is not lost. The people at Hurlant have created a library that does this "almost" as good as native JavaScript.
Here is a good example.
And another library example using d.eval
I would like to point out though that if you have accept to the source of the object string that you create a JSON object out of it. The JSON libraries are usually much easier and more reliable to use then the libraries that do Eval.
Your string is a sting with JSON format. Use JSONDecoder to decode it to an Object, like this:
var dc:JSONDecoder = new JSONDecoder("{a:1,b:'string',c:['one','two']}");
var ob:Object = dc.getValue();