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.
Related
In a Grails 2.5.X app, I can directly bind request data to a command object field of type Map like so:
class MyController {
def myAction(Command command) {
Map requestData = command.data
}
}
class Command {
Map data
}
It seems that internally Grails uses Gson for the parsing of the JSON data. If for example, the request data is {"page": 6} the corresponding Map will be
[page: new LazilyParsedNumber(6)]
i.e. the value stored in the Map is an instance of com.google.gson.internal.LazilyParsedNumber.
This is problematic for me. I would prefer if the Map were equivalent to that which would be created by:
new groovy.json.JsonSlurper().parseText('{"page": 6}')
which is:
[page: new Integer(6)]
I've looked into the various options for customising the databinding, and none of them hook into the pipeline sufficiently early. In other words, no matter which of the options I choose, the request data has already been processed by Gson.
Is it possible to replace Gson with JsonSlurper as the default parser of JSON request data?
Is it possible to replace Gson with JsonSlurper as the default parser
of JSON request data?
Yes, but it isn't something that we document or provide any particular support hooks for.
The default JSON data binding source creator is at https://github.com/grails/grails-core/blob/v2.5.6/grails-web-databinding/src/main/groovy/org/codehaus/groovy/grails/web/binding/bindingsource/JsonDataBindingSourceCreator.groovy.
An instance of that class is added to the Spring application context by the data binding plugin at https://github.com/grails/grails-core/blob/bd7cc10e17d34f20cedce979724f0e3bacd4cdb4/grails-plugin-databinding/src/main/groovy/org/codehaus/groovy/grails/plugins/databinding/DataBindingGrailsPlugin.groovy#L97.
One thing you could do is write your own class which extends AbstractRequestBodyDataBindingSourceCreator (or just implements DataBindingSourceCreator) and register an instance of that class as a bean named jsonDataBindingSourceCreator and that will replace the default one with yours. Then you are on your own to use whatever techniques you like for parsing the body of the request and creating a Map.
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;
I've got a simple Spring MVC app where controllers are generating Json objects and returning them as Strings.
I'd like to return those json trees as-is out of the controllers and have a kind of servlet filter which will enrich them. Basically it will take the json node and move it as a child of a brand new json root. Think of a 'body' encapsulated in a complete response including also a 'head' child node that would be generated by this filter.
It is possible to do so within Spring ?
Thanks for your advices !
I would advise instead of returning plain Strings, return POJO and convert this to JSON using HTTP Message converters (e.g. MappingJackson2HttpMessageConverter) and #ResponseBody annotation. Then you can decorate the return object the way you want.
I need to generate Json from the domain object. I can't add annotation in the domain classes.
Using Mixin is not a option because I have to ignore a lot of properties.
My approach was create a DTO object with the properties that I need. And populate the dto using dozer and then generate Json from the dto with jackson. It looks too much.
I would like to know if is posible to configure Jackson from xml, in order to generate json with the properties mapped in the xml, so it would not be necessary to use dto and dozer.
No. Jackson does not support external configuration files.
But you don't explain how or why you would use Dozer, or DTO. Why not just add properties you care about in a Map, and serialize that as JSON? Then you can use whatever mechanism you want to build/trim that Map.
Jackson can also convert values, so to create full Map with everything from another object, you can do:
Map<String,Object> map = objectMapper.convertValue(someBean, Map.class);
and maybe then only retain properties you want.
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.