How to read a value in Dynamic object obtained from JsonConvert.DeserializeObject - json.net

I am using httpClient to an API endpoint. There are a couple of possible JSON results I could get back depending on if the call if successful or not.
So I want to use a dynamic or ExpandoObject to DeserializeObject the results to.
However, I can't seem to be able to actual get anything from the object after Deserializing. I keep getting `
error CS1061: 'object' does not contain a definition for 'success' and no accessible extension method 'success' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)'
Here is a simplified example:
dynamic stuff = JsonConvert.DeserializeObject<ExpandoObject>("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
When I try to access name like this stuff.name I get the following error:
error CS1061: 'object' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
? stuff["name"]
error CS0021: Cannot apply indexing with [] to an expression of type 'object'
And yet the data is in there somewhere:
If I Deserialize like this I get the exact same result:
dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");
It was inferred in another post that it may have something to do with this being in an async function, but no real solution. I am running .Net core 3.1 from within an Azure function.
So how do I refer to the object to get a value out?

Related

Structural error property type should be equal to one of the allowed values allowedValues: string, number, boolean, integer, array in Swagger editor

I'm using springfox 2.9.2 and want test my swagger JSON as YAML in https://editor.swagger.io/
I have property with #ApiParam annotation type: object
#ApiParam(value = "metadata file")
protected Object metadataFile;
but when I test my generated json on swagger editor I got this error:
Structural error at ---.parameters.5.type should be equal to one of
the allowed values allowedValues: string, number, boolean, integer,
array Jump to line ---
there is way to allowed property type object in this section?
the section that trigger the problem
paths:
:
post:
parameters:
name: metadataFile
in: query
description: ...
required: false
type: object
Swagger documentation (go to the bottom of the page): https://swagger.io/docs/specification/2-0/describing-parameters/
"Can I have an object as a query parameter?
This is possible in OpenAPI 3.0, but not in 2.0."

Swagger API definition load API definition - Cant use schema id "$Module"

I am getting this exception when I want to create a Domain object with name Module
InvalidOperationException: Can't use schemaId "$Module" for type "$Pro.Core.Domain.Module". The same schemaId is already used for type "$System.Reflection.Module"
Swashbuckle.AspNetCore.SwaggerGen.SchemaRepository.RegisterType(Type type, string schemaId)
Seems like this name Module is conflicting with some System.Reflection.Module.
I have searched the internet and I have below 2 solutions that can get this working:
Rename my Module class name to something else but not Module(lets say MyModule)
Doing something like below
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(config =>
{
//some swagger configuration code.
//use fully qualified object names
config.CustomSchemaIds(x => x.FullName);
}
}
But I want to understand that what is there in name Module that creates this error. I don't see any reason for not being allowed to use this name as my domain class name.
Is there any way to use the Domain Object with name Module other that mentioned above in point 2 and why is this happening in the first place?
Fixed by using
services.AddSwaggerGen(options =>
{
options.CustomSchemaIds(type => type.ToString());
});
Find more details from
here

Meteor Iron:Route with Meteor 1.2

I'm learning Meteor by following this tutorial.
Here is my git https://github.com/nicholaschong12/meteor-slackclone
I'm getting this error:
Uncaught TypeError: Cannot read property 'channel' of undefined
Uncaught TypeError: Function.prototype.apply: Arguments list has wrong
type
Meteor.startup(function(){
Session.set('channel',this.params.channel);
});
Please help me to understand this error.
Thank You
Looking at the error:
Uncaught TypeError: Cannot read property 'channel' of undefined
We can see that it is trying to read a property 'channel' of an undefined object. This object from your code is:
this.params
Reading the iron:router docs, tells us that:
When a user goes to that url, the actual value of the parameter will be stored as a property on this.params in your route function.
However in the code snippet you provided:
Meteor.startup(function(){
Session.set('channel',this.params.channel);
});
You are trying to access this.params inside a Meteor.startup callback function, not a Router.route function. In this context this.params is undefined, as the error is telling you.
Change your startup code to match the tutorial:
Meteor.startup(function() {
Session.set('channel', 'general');
});
And in your routing code you can use values from the route like this:
Router.route('/:channel', function () {
Session.set('channel', this.params.channel);
this.render('messages');
});
In this context this.params will return an object, including the channel property defined in the routes path.

ASP.NET vNext Core CLR missing type.IsPrimitive

I am trying to migrate a web app to ASP.Net vNext with the eventual aim of getting it running on Linux.
The app has a lot of reflection code and I must be missing some dependencies as I am getting compile errors on code such as this
Type.IsPrimitive, Type.GetConstructor Type.GetMethod Type.GetTypeArray
Error CS1061 'Type' does not contain a definition for 'IsPrimitive' and no extension method 'IsPrimitive' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
Error CS1061 'Type' does not contain a definition for 'GetMethod' and no extension method 'GetMethod' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
Error CS1061 'Type' does not contain a definition for 'GetProperties' and no extension method 'GetProperties' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
Error CS1061 'Type' does not contain a definition for 'GetInterface' and no extension method 'GetInterface' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
I have the following dependencies in my project.json files
"frameworks" : {
"aspnetcore50" : {
"dependencies": {
"System.Runtime": "4.0.20-beta-22416",
"System.Linq": "4.0.0.0-beta-22605",
"System.Reflection": "4.0.10.0-beta-22605",
"System.Reflection.Primitives": "4.0.0.0-beta-22605",
"System.Runtime.Extensions": "4.0.10.0-beta-22605",
"System.Reflection.Extensions": "4.0.0.0-beta-22605"
}
The following compiles fine under VS 2013 and .Net 4.5 but wont compile in VS 2015 using the dependencies above
using System;
using System.Reflection;
namespace Project1
{
public class Class1
{
public Class1()
{
Type lBaseArrayType = typeof(Array);
Type lStringType = typeof(string);
string[] lStringArray = new string[1];
if (lStringType.IsPrimitive)
{
}
ConstructorInfo lConstructor = lStringType.GetConstructor(new Type[0]);
MethodInfo lMethod = lStringType.GetMethod("Equals");
Type[] lTArray = Type.GetTypeArray(lStringArray);
PropertyInfo[] lProps = lStringType.GetProperties();
}
}
}
If you're using aspnetcore .IsPrimitive is available, but not as a member of Type. You'll find it under TypeInfo, which can be accessed by calling the GetTypeInfo() method of Type. In your example, it would be:
lStringType.GetTypeInfo().IsPrimitive
Type.GetMethod() is also available, but you'll need to reference the System.Reflection.TypeExtensions package in your project.json file.
Type.GetTypeArray() is missing, but you can easily write a simple linq query to retrieve an array of member types in the array.
Type.GetInterface() is not included, but again using System.Reflection.TypeExtensions will expose another method that generates a Type[] of all implemented interfaces for the type specified.
Type[] types = Type.GetInterfaces()
Type.GetProperties() is available again through the System.Reflection.TypeExtensions library.
You can use the static method System.Reflection.IntrospectionExtensions.GetTypeInfo() to get the same information.
var arrayType = typeof(Array);
var stringType = typeof(string);
var stringArray = new string[1];
var stringTypeInfo = stringType.GetTypeInfo();
if (stringTypeInfo.IsPrimitive)
{
}
ConstructorInfo lConstructor = stringTypeInfo.DeclaredConstructors.Where(x => x.GetParameters().Any() == false).FirstOrDefault();
MethodInfo lMethod = stringTypeInfo.DeclaredMethods.Where(x => x.Name == "Equals").FirstOrDefault();
Type[] lTArray = stringArray.Where(x => x != null).Select(x => x.GetType()).Distinct().ToArray();
PropertyInfo[] lProps = stringTypeInfo.DeclaredProperties.ToArray();

Specifying a root node of FormPanel.form.load for dynamic forms

I'm building a dynamic ExtJS form based on JSON data loaded from an ASP.NET web service. The problem I find is that ExtJS expects the JSON in a specific format, ie.
{ "metaData": { "title": "Testing" }, "data": [], "success": true }
When using an ASP.NET web service to return an object as JSON it returns with the first element "d", ie.
{ "d": { "metaData": { "title": "Testing" }, "data": [], "success": true } }
Is it possible to tell the ExtJS form to use "d" as the root node?
After some more testing I've found that the ExtJS form does load the JSON from my web service but because the response text doesn't have "success": true in the root it is handled by the 'failed' handler. Fortunately this handler accepts the same parameters as the 'success' handler so can be manipulated the same.
Here's an example of my form load handler:
this.form.load({
url: "myservice.asmx/getUser",
headers: {'Content-Type': 'application/json'},
success: function(form, action) {
//not fired
},
failure: function(form, action){
if (action.result.d){
//process data
}
}
});
You don't have to call form.load() of course. I bypass it, and simply call my ASMX web method directly by calling the AJAX function that links to my webmethod, as provided by the ScriptManager. MS AJAX does all the JSON decoding, and factors out the 'd' property, etc.
Your web method doesn't even have to return an object with the 'success' and 'data' objects, as required by form.load(), although it's a useful format and I stick to it.
With the 'data' object returned by the web method (with name/value pairs, where name == field name) you can now call ExtJs's form.setValues(data); to write the values into the fields.
This is a perfectly valid case for bypassing ExtJS's code.
--
As with loading, so with submitting. To get around the 'd' property problem in the object that has to be returned by the submission web method, handle the click event of the Submit button, and push the data to the server by directly calling your web method. Your web method can/should return an object in the same format as is required by ExtJs. But you get back this object, and if not successful, call form.markInvalid() yourself, passing in the 'errors' property. Peasy easy and works well.
Again, since ExtJs doesn't play nice with the 'd' property it's perfectly valid to bypass it and do things yourself.
--
I tend to use the ScriptManager-provided functions for calling my web methods more and more, and bypass ExtJs's AJAX method invoking code. The former are much simpler to use, know about the 'd' property and also know how to deserialize Microsoft's JSON format for serialized DateTime objects.

Resources