Is there an elegant way to convert an object that contains prototypes to an object with properties only? (all prototype methods would be omitted)
In one line:
_.omit(myObj, _.isFunction);
Also with JSON:
JSON.parse(JSON.stringify(myObj));
https://jsfiddle.net/yh2utkdw/1/
Related
I have a string object like this. I have tried deserializing using newtonsoft json converter but it results in the null object. I got to know the reason behind which is that the object I tried to convert to is inside the "data". Hence, it returns Null always for each property after conversion.
I want to know how I can access the "data" directly from this object?
{"data":{"providerRef":null,"orderId":"4579144","orderStatus":"x:app_pending","applicantInterfaceURL":"http://google.com","successful":true,"error":null,"reportAddress":null,"correlationId":"55f7022c-28f9-490a-8dd1-b30a40e3467a"},"status":0,"error":{"actionArguments":null,"errorCode":null,"errors":null,"message":null}}
Now, I have implemented it like
The class VolunteerBackgroundCheckResponse have these properties:
Can anyone help me getting through the data object only from the json string?
This is solved. I have used the JObject for the purpose and received all the properties beneath the main object. I accessed "data" and deserialized it.
I have a Mongo collection annotated with #Document and I want the possibility to also get that Java object from a String (JSON) as we're getting these classes pushed into a queue as String.
Is there a method in Spring-Data-Mongo which converts from JSON to the actual Document object?
#Autowired
MongoTemplate mongoTemplate;
and then
mongoTemplate.getConverter().read(MatchMongo.class, (DBObject) JSON.parse(json));
Thanks to freakman, your answer helped a lot
You can try com.mongodb.util.JSON.parse() method. It returns object so you probably have to do the casting + it may be it need "class" field inside json string.
RealBasic's Introspection is kinda of different than what I expected.
My intention is:
Create a MainObject from which other objects will inherit two, three methods, to simplify.
Method 1-> Returns to the child class itself all of its properties, types and values.
Method 2-> Would call Method1 and with the information, save the child Object.
So, for method 1 I thought about writing a generalised introspection which for each child class would easily return what I need for Method 2 to do its work.
Why do I want this? So I can have tens of objects knowing how to save, draw themselves without worrying too much about a modification here or there on the properties etc...
But using what RealBasic tutorials and reference offers doesn't work, since it requires me to have it happening outside the object etc... i.e.: I can easily, inside ObjectA, get ObjectB's properties, methods etc, but I want to get inside ObjectA, A's properties, not B's
Thanks in advance...
I've found out how... Very simple, create the MainClass and inside of it a simple method WhoAmI which could return an array, dictionary etc...
Dim thisClassTypeInfo As Introspection.TypeInfo = Introspection.GetType(Self)
Dim thisClassProperties() As Introspection.PropertyInfo = thisClassTypeInfo.GetProperties
Dim thisClassMethods() As Introspection.MethodInfo = thisClassTypeInfo.GetMethods
For Each myProperty As Introspection.PropertyInfo In thisClassProperties
// Then here use myProperty.Name, myProperty.Value(Self).StringValue
// or myProperty.Value(Self).anyotheroption and create a dictionary
// or array with the results and return it.
Next
I am trying to build a createEntity OEntity for an object that has multiple child collections within it.
I have looked over many of the example projects, but they all seem to assume that you have a known number of child objects in a collection so that you can use .inLine(“ObjectName”, ObjectOEntity1, ObjecteOEntity2…)
I have tried looking at the documentation and have not identified anything that leads me to think I can create a collection of OEntity objects that can then be added to my parent object with the inline.
The closest I found was the example listed on:
http://code.google.com/p/odata4j/source/browse/odata4j-fit/src/test/java/org/odata4j/producer/jpa/northwind/test/CreateTest.java?name=0.6
Has anyone else run into this problem?
If so how did you get around it?
You can pass in an array of OEntity objects. The core4j library that is used by odata4j contains some helper methods that can - for example - be used to get an array from an Iterable:
OEntity[] entitiesArray = Enumerable.create(entitiesIterable)
.toArray(OEntity.class);
But as there are also two variants of the properties method...
OCreateRequest<T> properties(OProperty<?>... props);
OCreateRequest<T> properties(Iterable<OProperty<?>> props);
... it might make sense to add an inline method that directly takes an Iterable<OEntity>.
Ok so I'm trying to use the JavaScriptSerializer to work with this code. However it crashes when it reaches the last line;
Dim json As New String(sr.Serialize(dt))
I get this error message;
A circular reference was detected
while serializing an object of type
'System.Reflection.Module'.
I would really appreciate any insights that could help solve this problem.
Circular reference means that serialising the object would result in an infinite loop.
For example if you would try to serialize object "A" having a 1 to 1 reference to object "B".
Declare a class containg the data you want to serialize with JSON to solve this issue.
As hkda150 has already said, you could use a class specifically tailored for being serialized.
This will furthermore enable you to have foreign key values serialized instead of having related full objects serialized. Thus, if you are serializing object a which has a property a.SomeB of type B, then you will often want the ID of a.someB to be present in your webpage. Obviously I don't know enough to be able to say if this is relevant in your specific use case.
BTW, If you find yourself doing a lot of mapping between "business objects" and "objects meant for serialization" you may want to consider using AutoMapper.