Environment
.net 7
Using Both System.Text.Json
Also NewtonSoft.Json ( 13.0.2)
Example code
string str = #"{
""DateTimeNull"":""""
}";
try
{
var t = System.Text.Json.JsonSerializer.Deserialize<Test>(str);
}
catch (JsonException ex)
{
Console.WriteLine(new { Field = ex.Path , Message = ex.Message });
}
try
{
var t = Newtonsoft.Json.JsonConvert.DeserializeObject<Test>(str);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
public class Test
{
public DateTime? DateTimeNull { get; set; }
}
In above System.Text.Json Deserlizer throw exception but newtonsoft.json line is not throwing any exception. It is converting empty value to null but I want it should thow error and due to limitation I can not move to System.Text.Json as of now.
Payload ( This is i already set in str)
Sample one
#"{
""DateTimeNull"":""""
}";
Expected result: Throw error and should not convert to null.
Sample two.
#"{
""DateTimeNull"": null
}";
Expected result: Should not throw error and it is null value and destination type is null.
I usually recommend to use a JsonConstructor:
var json = #"{
""DateTimeNull"":""""
}";
Test test = JsonConvert.DeserializeObject<Test>(json);
public class Test
{
public DateTime? DateTimeNull { get; set; }
[Newtonsoft.Json.JsonConstructor]
public Test(JToken DateTimeNull)
{
if (DateTimeNull.Type == JTokenType.Null) this.DateTimeNull = null;
else if ((string)DateTimeNull == string.Empty)
throw new JsonException("DateTimeNull property should not be an empty string");
else this.DateTimeNull = DateTimeNull.ToObject<DateTime>();
}
}
Related
I saw the implementation of StringUtils.isBlank() and was surprised that in my case, it is not working as expected.
I was writing a Junit and I faced this issue.
AmazonSNSRegistrationService.java:
public String doAWSSNSNDeviceRegistration(Map<String, Object> jsonBody){
String deviceId = (String) jsonBody.get(PushNotificationsConstants.DEVICE_ID);
String pushToken = (String) jsonBody.get(PushNotificationsConstants.PUSH_TOKEN);
String appId = (String) jsonBody.get(PushNotificationsConstants.APP_ID);
if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(pushToken) || StringUtils.isBlank(appId)) {
System.out.println("$$$$$ Empty");
throw new Exception("Required parameters are empty.");
}
return registerWithSNS(pushToken, deviceId, appId);
}
AmazonSNSRegistrationServiceTest.java:
#Test
public void doAWSSNSNDeviceRegistrationBlankTest() {
Map<String, Object> jsonBody = new HashMap<String, Object>();
jsonBody.put(PushNotificationsConstants.APP_ID, "");
jsonBody.put(PushNotificationsConstants.DEVICEID, " ");
try{
amazonSNSRegistrationService.doAWSSNSNDeviceRegistration(jsonBody);
}
catch(Exception e){
}
}
I'm not passing Push Token , so it will be null. Other values are either "" or " ".
But in if condition, none of the statements give true. All return false and the Exception is not thrown. I want this code to throw exception when any of the statement in if (StringUtils.isBlank(deviceId) || StringUtils.isBlank(pushToken) || StringUtils.isBlank(appId)) returns true.
I'm trying to implement my own custom exceptions in dot net core.
This is what I have so far:
public class WSException: Exception
{
// some custom stuff...
private readonly string _developerMessage = "";
public string DeveloperMessage { get { return _developerMessage; } }
public WSException() {}
public WSException(string message) : base(message) {
this._developerMessage = message;
}
public WSException(string message, Exception inner) : base(message, inner) {
this._developerMessage = message;
}
public WSException(Exception ex) : base(ex.Message, ex.InnerException) {
_developerMessage = ex.Message;
Source = ex.Source;
//StackTrace = ex.StackTrace; // cannot be assigned to, it's read only
}
public WSException(string message) : base(message) {
this._developerMessage = (String.IsNullOrWhiteSpace(developerMessage) ? message : developerMessage);
}
}
When I catch a general exception, I try to create one of my own (a WSException) to handle it in a common way, like this:
try {
// whatever
}
catch (WSException e) {
HandleException(e);
}
catch (Exception e) {
HandleException(new WSException(e));
}
When I do it like that, e.Source and e.StackTrace are null, and when I try to assign StackTrace I get a Propery or indexer 'Exception.StackTrace' cannot be assigned to --it is read only.
How should such I implement this constructor?
public WSException(Exception ex) : base(ex.Message, ex.InnerException) {
_developerMessage = ex.Message;
Source = ex.Source;
//StackTrace = ex.StackTrace; // cannot be assigned to, it's read only
}
The workaround I found so far is to handle it when I'm serializing the error to json, something like this:
public class WSExceptionJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var ex = value as WSException;
writer.WriteStartObject();
// buch of properties[...]
string stackTrace = null;
if (ex.StackTrace != null) {
stackTrace = ex.StackTrace;
} else if (ex.InnerException != null && ex.InnerException.StackTrace != null) {
stackTrace = ex.InnerException.StackTrace;
} else {
stackTrace = null;
}
writer.WritePropertyName("stacktrace");
serializer.Serialize(writer, stackTrace.Split('\n'));
writer.WriteEndObject();
}
But it feels too hacky
With the below code I have converted list to json but the format is as
follows:
{"GodownMaster":[{"pname":"FCI CHARLAPALLI","pcode":"16042"},
{"pname":"MLS CIRCLE 1 L.B. NAGAR","pcode":"16016"},{"pname":"MLS CIRCLE 4
AZAMABAD","pcode":"16003"},{"pname":"MLS CIRCLE 6
VIDYANAGAR","pcode":"16005"},{"pname":"OTHERS","pcode":"1699"}]}
but I want to convert it as :
[{"pname":"FCI CHARLAPALLI","pcode":"16042"},
{"pname":"MLS CIRCLE 1 L.B. NAGAR","pcode":"16016"},{"pname":"MLS CIRCLE 4
AZAMABAD","pcode":"16003"},{"pname":"MLS CIRCLE 6
VIDYANAGAR","pcode":"16005"},{"pname":"OTHERS","pcode":"1699"}]
Below is my spring controller :
#RequestMapping("/getGodowns")
public #ResponseBody Map
getGodownsBasedOnDistrict(#RequestParam(value="district_code") String
dist_code) {
List<CscGodownBean> godown_list = null;
Map<String, List<CscGodownBean>> m = new HashMap();
String exception = null;
try
{
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
ex.printStackTrace();
exception = ex.getMessage();
}
if(godown_list!=null) {
for(int i=0;i<godown_list.size();i++) {
m.put("GodownMaster",godown_list);
}
}
return m;
}
Why are you putting your list into Map? Code looks weird. If you want to return a list, just do it:
#RequestMapping("/getGodowns")
public #ResponseBody List<CscGodownBean> getGodownsBasedOnDistrict(#RequestParam(value="district_code") String dist_code) {
List<CscGodownBean> godown_list = null;
String exception = null;
try {
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
} catch (Exception ex) {
ex.printStackTrace();
exception = ex.getMessage();
}
return godown_list;
}
Change the return result from Map to List<CscGodownBean> and put : retrun godown_list
So;
#RequestMapping("/getGodowns")
public #ResponseBody List<CscGodownBean>
getGodownsBasedOnDistrict(#RequestParam(value="district_code") String
dist_code) {
List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
String exception = null;
try
{
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
ex.printStackTrace();
exception = ex.getMessage();
}
return godown_list ;
}
UPDATE
And you can return result as string and you will get what you need :
#RequestMapping("/getGodowns")
public #ResponseBody String
getGodownsBasedOnDistrict(#RequestParam(value="district_code") String
dist_code) {
List<CscGodownBean> godown_list = new ArrayList<CscGodownBean>();
String exception = null;
try
{
//getting name and codes here
godown_list = scm_service.getGodownListBesedOnDistCode(dist_code);
}catch(Exception ex)
{
ex.printStackTrace();
exception = ex.getMessage();
}
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
String arrayToJson = objectMapper.writeValueAsString(godown_list);
System.out.println("Convert List to JSON :");
System.out.println(arrayToJson);
return arrayToJson ;
}
The returned String is json format.
Here is what I have used:
#RequestMapping("/alluserreportJSON")
public #ResponseBody String getusersJSON() {
ObjectMapper objectMapper = new ObjectMapper();
//Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
List<AppUser> userlist = null;
#SuppressWarnings("unused")
String exception = null;
String arrayToJson = null;
try {
userlist = userService.findAllUsers();
arrayToJson = objectMapper.writeValueAsString(userlist);
} catch (Exception ex) {
ex.printStackTrace();
exception = ex.getMessage();
}
return arrayToJson;
}
Hope it helps someone. You can see it working here.
You add return type as Map, still you want the same then Just in ajaxComplete() put code;
var response = '{"GodownMaster":[{"pname":"FCI CHARLAPALLI","pcode":"16042"}, {"pname":"MLS CIRCLE 1 L.B. NAGAR","pcode":"16016"},{"pname":"MLS CIRCLE 4 AZAMABAD","pcode":"16003"},{"pname":"MLS CIRCLE 6 VIDYANAGAR","pcode":"16005"},{"pname":"OTHERS","pcode":"1699"}]}'
JSON.stringify(JSON.parse(response).GodownMaster);
What could be the reason of this error in the code below?
loginButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick (View v){
final String e_mail = e_mailEditText.getText().toString();
final String password = passwordEditText.getText().toString();
// Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
// int age = jsonResponse.getInt("age");
Intent intent = new Intent(login.this, Welcome.class);
intent.putExtra("name", name);
// intent.putExtra("age", age);
intent.putExtra("e_mail", e_mail);
login.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(e_mail, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(login.this);
queue.add(loginRequest);
}
});
Check if you have the key first:
if (jsonObject.has("name")) {
String name = jsonObject.getString("name");
}
For others users which have the org.json.JSONException: No value for //your parameter.
In this case you should check if the name is empty.
For example using method jsonResponse.optString("name").
Live example:
if (success) {
String name = jsonResponse.optString("name"); //will get name value or return empty String
if (!name.equals("")) {
//Your code if name is exist
Intent intent = new Intent(login.this, Welcome.class);
intent.putExtra("name", name);
intent.putExtra("e_mail", e_mail);
login.this.startActivity(intent);
} else {
//Your code if the name is empty
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(login.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
Can't say for sure without knowing the context (or the line number of the exception), but my money would be on the call:
jsonResponse.getString("name")
Most likely, the JSON received from the server doesn't contain any name/value pairs with name name.
I have webapi, that return JArray.
There is any way to send response with some status code that I pick (like 422, 4XX )?
//GET api/UserControl/GetUserName
public JArray GetUserName()
{
JArray json = new JArray();
try
{
string UserID= getUserID();
if (!string.IsNullOrEmpty(UserID) || UserID== "None Was found")
{
var result = JsonConvert.SerializeObject(donorRep.GetUserFullName(UserID), Formatting.None,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
try
{
json = JArray.Parse(result);
}
catch
{
json.Add(result.ToString());
}
}
else
{
json.Add("There was an issue while retrieving your UserID.");
}
}
catch (JsonSerializationException ex)
{
json.Add("There was an issue while retrieving your IDSID. Please contact support");
}
return json;
}
for example if there is an error here than in the UI :
json.Add("There was an issue while retrieving your UserID");
Wrap your array inside a JSON object, then add additional properties for the status code and any other "meta" information you might need. For example, you might try to make the JSON response look something like this:
{
"statusCode" : 422
"errorMessage" : "Error message, if any, goes here."
"results" : [
"item 1",
"item 2",
"etc.",
"you can also use objects here instead of strings if your data is more complex"
]
}
Then make your code something like this:
// GET api/UserControl/GetUserName
public JObject GetUserName()
{
JArray resultArray = new JArray();
int statusCode = 200; // success
string errorMessage = null;
try
{
string UserID= getUserID();
if (!string.IsNullOrEmpty(UserID))
{
var fullName = donorRep.GetUserFullName(UserID);
resultArray.Add(fullName);
}
else
{
statusCode = 421; // error
errorMessage = "There was an issue while retrieving your UserID.";
}
}
catch (Exception ex)
{
statusCode = 422; // error
errorMessage = "There was an issue while retrieving your IDSID. Please contact support.";
}
JObject response = new JObject();
response.Add("statusCode", statusCode);
response.Add("errorMessage", errorMessage);
response.Add("results", resultArray);
return response;
}
You'll have to adjust your client side code to be able to extract the parts of the response. If you're using jQuery, for example, you could get the data something like this:
$.get("/api/UserControl/GetUserName")
.done(function(data) {
var statusCode = data.statusCode;
if (statusCode == 200) {
var userName = data.results[0];
alert("Success! User name is " + userName);
}
else {
alert("Failed with code " + statusCode + ". message: " + data.errorMessage);
}
});
For sake of completeness I should also mention that in Web API you don't have to manually build the JSON using JObjects, JArrays, etc. As an alternative, you can use strongly-typed classes and return those from your methods directly, and Web API will serialize them to JSON for you. Of course the structure of the classes has to match the JSON you want to return. For example if you wanted to do that approach, you would define a class like this:
class ResponseData
{
public int statusCode { get; set; }
public string errorCode { get; set; }
public List<string> results { get; set; }
public ResponseData()
{
results = new List<string>();
}
}
Then you can do:
public ResponseData GetUserName()
{
ResponseData response = new ResponseData { statusCode = 200 };
try
{
string userID = getUserID();
if (!string.IsNullOrEmpty(UserID))
{
var fullName = donorRep.GetUserFullName(UserID);
response.results.Add(fullName);
}
else
{
response.statusCode = 421; // error
response.errorMessage = "There was an issue while retrieving your UserID.";
}
}
catch (Exception ex)
{
response.statusCode = 422; // error
response.errorMessage = "There was an issue while retrieving your IDSID. Please contact support.";
}
return response;
}