Unable to Iterate over Hashmap using java8 - iterator

The below given is snipet of the ccode i am facing trouble with and i am using jdk 8. I am facing error in the bold line of the code , for loop statement. I have mentioned the error too.:
do {
jobid = br.readLine();
metajson = br.readLine();
JSONObject obj = (JSONObject) jsonParser.parse(metajson);
System.out.println(jobid+" "+obj.toString());
//The below one should work
****for (HashMap.Entry<String, String> entry : obj.entrySet())****
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
}
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
PropertyEntry cannot be resolved to a type
Duplicate local variable entry
Entry cannot be resolved to a type
at com.journaldev.json.Insert3.main(Insert3.java:64)
a error at this line that says "Type mismatch: cannot convert from element type Object to Map.Entry>"
I tried the Property.Map() and concept.Map() too, but the same issue is there. I also imported the whole collection class too. But i don't know the error is not resolving.

Because the returned set of entries has a type of <String, JSONObject> instead of <String, String> .
The following code compiles:
Set<Entry<String, JSONObject>> entrySet = jsonObject.entrySet();
for (Entry<String, JSONObject> entry : entrySet) {
String key = entry.getKey();
JSONObject innerJsonObject = entry.getValue();
}
Anyway that library (simple-json) has a bad design. JsonObject inherits itself from HashMap raw type (without filling it's generic type declaration) so the resulting entrySet's type is not known at compile time.
Tip: Use GSon or other json library instead of that.

Related

.NET6/EF: The best overloaded method match for 'Microsoft.EntityFrameworkCore.DbSet<...>.Add(...)' has some invalid arguments

I'm working on a generic code to add .NET 6 Entity Framework DbSet<...> records, deserialized from JSON strings. The original code is (much) more elaborated, below are just samples to demonstrated the issue - the following method:
public static void AddRecord(dynamic dbSet, Type entityType, string json)
{
var dataRecord = System.Text.Json.JsonSerializer.Deserialize(json, entityType);
dbSet.Add(dataRecord);
}
results in a run-time error at dbSet.Add(dataRecord) call:
"The best overloaded method match for
'Microsoft.EntityFrameworkCore.DbSet<Northwind.Models.Category>.Add
(Northwind.Models.Category)' has some invalid arguments"}
...
This exception was originally thrown at this call stack:
System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2<T0, T1>
(System.Runtime.CompilerServices.CallSite, T0, T1)
if you call it, e.g., this way:
using (var ctx = ...)
{
...
var json = ...
...
AddRecord(ctx.Categories, typeof(Category), json);
}
I have intentionally, for clarity, used in the above code concrete dbSet (ctx.Categories) and compile time typedef (typeof(Category)) - in actual code these are run-time defined variables.
If you "unroll" the method code and write it this way:
using (var ctx = ...)
{
...
var json = ...
...
var dataRecord = System.Text.Json.JsonSerializer.Deserialize(json, typeof(Category));
ctx.Categories.Add(dataRecord);
}
you would still get the mentioned above run-time error for the .Add method.
But if you write:
var dataRecord = System.Text.Json.JsonSerializer.Deserialize<Category>(json);
ctx.Categories.Add(dataRecord);
or
var dataRecord = System.Text.Json.JsonSerializer.Deserialize(json);
ctx.Categories.Add((Category)dataRecord);
the code will work without any issues.
Finally, an attemp to use Convert.ChangeType doesn't help:
var dataRecord = System.Text.Json.JsonSerializer.Deserialize(json);
ctx.Categories.Add(Convert.ChangeType(dataRecord, typeof(Category)));
So, it looks like an explicit object type casting is compiled to and makes on rum-time some "special object interfaces arrangements", which dynamic object type casting doesn't?
[Update]
Okan Karadag's prompt answer below gave me a hint how to change AddRecord(...) method to workaround the subject issue:
public static void AddRecord(DbContext dbContext, Type entityType, string json)
{
var dataRecord = System.Text.Json.JsonSerializer.Deserialize(json, entityType);
dbContext.Add(dataRecord);
}
This method works flawlessly. Although it doesn't answer the subject question, why the original AddRecord(...) method
public static void AddRecord(dynamic dbSet, Type entityType, string json)
{
var dataRecord = System.Text.Json.JsonSerializer.Deserialize(json, entityType);
dbSet.Add(dataRecord);
}
results in 'The best overloaded method match for 'Microsoft.EntityFrameworkCore.DbSet<...>.Add(...)' has some invalid arguments...' runtime error at
dbSet.Add(dataRecord);
code line.
You can use generic for dynamic.
public void AddEntity<T>(string json) where T:class
{
var entity = JsonSerializer.Deserialize<T>(json);
ArgumentNullException.ThrowIfNull(entity);
dbContext.Add<T>(entity);
dbContext.SaveChanges();
}
You can see link for problem error.

Jersey response.readEntity(...) sometimes returns null

Consider this snippet from my REST client (Jersey 2.26). It's used to post objects and return the response object. If the REST server returns an error (status >= 400), then instead of returning an entity of type T I want to read an entity of type ErrorMessage and throw an exception containing the error message object.
protected <T> T post(final Class<T> type,
final Object entity,
final Map<String, Object> queryParams,
final String methodPath,
final Object... arguments) {
return postResponse(
getInvocationBuilderJson(methodPath,
queryParams,
arguments),
entity
).readEntity(type);
}
protected Response postResponse(final Invocation.Builder invocationBuilder,
final Object entity) {
return handleErrors(
invocationBuilder.post(Entity.entity(entity,
MediaType.APPLICATION_JSON_TYPE))
);
}
protected Response handleErrors(final Response response) {
if (response.getStatus() >= 400) {
throw new InvocationException(response.readEntity(ErrorMessage.class));
}
return response;
}
If no error occurs (status < 400), then my object of type T is returned as expected. However, when an error does occur, response.readEntity(ErrorMessage.class) returns null. But (and this is the strange part), this does get me data (at the handleErrors method):
byte[] data = readAllBytes((InputStream) response.getEntity());
I could use that and deserialize it manually.. but I would first like to know if there are any options to fix this without implementing workarounds.
After switching from the default MOXy JSON (de)serializer (we now are using a GSON provider) the problem was resolved.
We recently had a similar issue with JSON-B. There is turned out we had to add a getter and setter on our error object on order to (de)serialize the object.

Hibernate Custom SQL Enum transformation fails

There are similar questions here but none of them worked in my case. I have a custom SQL which returns 2 columns: one string, one number. And string column is always a full uppercase ENUM name. I want to feed this result set into my custom bean which has the said enum.
Following the answer here for Hibernate 5.X, code is below
Properties params = new Properties();
params.put("enumClass", "MyEnumClass");
params.put("useNamed", true);
Type myEnumType = new TypeLocatorImpl(new TypeResolver()).custom(MyEnumClass.class, params);
final Query query = getCurrentSession().createSQLQuery(MY_CUSTOM_SQL)
.addScalar("col1", myEnumType)
.addScalar("col2", StandardBasicTypes.INTEGER)
.setLong("someSqlVar", someVal)
.setResultTransformer(Transformers.aliasToBean(MyCustomBean.class));
return query.list();
This code does not even execute query.list() method, it fails at this line:
Type myEnumType = new TypeLocatorImpl(new TypeResolver()).custom(MyEnumClass.class, params);
Exception trace:
Caused by: org.hibernate.MappingException: Unable to instantiate custom type: com.example.MyEnumClass
...
Caused by: java.lang.InstantiationException: com.example.MyEnumClass
...
Caused by: java.lang.NoSuchMethodException: com.example.MyEnumClass.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[?:1.8.0_60]
at java.lang.Class.newInstance(Class.java:412) ~[?:1.8.0_60]
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:202) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.type.TypeFactory.custom(TypeFactory.java:193) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
at org.hibernate.internal.TypeLocatorImpl.custom(TypeLocatorImpl.java:144) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
...
So hibernate is trying to call MyEnumClass.class.newInstance() and failing. It does not even check for properties I passed. Using Hibernate 5.1.0.Final, am I not supposed to use custom type this way?
I found a way to do it:
Properties params = new Properties();
params.put("enumClass", MyEnumClass.class.getName());
params.put("useNamed", true);
EnumType enumType = new EnumType();
enumType.setParameterValues(params);
CustomType customType = new CustomType(enumType);
final Query query = getCurrentSession().createSQLQuery(MY_CUSTOM_SQL)
.addScalar("col1", customType)
.addScalar("col2", StandardBasicTypes.INTEGER)
.setLong("someSqlVar", someVal)
.setResultTransformer(Transformers.aliasToBean(MyCustomBean.class));
return query.list();

FileHelper library - custom error messages

I am use the FileHelper to parse CSV files. Error messages encountered when parsing the file are displayed to the end user. The end user may not be able to make sense of the technical error message. Not too many clerks know what an Int32 is or Class: UploadFooDto.
I would like to customize the error messages so they are more user friendly. Something like:
Line 1. Column 2. A string (a) was entered instead of a number
Line 2. Column 3. '13-14-15' is not a valid date
I cannot find anything in the API that would allow me to customize the error messages. The most I have so far are some extension methods to clean up the errors:
public static class FileHelperExceptionExtensions
{
public static string BuildMessage(this ErrorInfo error)
{
if (error.ExceptionInfo is ConvertException)
{
return ((ConvertException)error.ExceptionInfo).BuildMessage();
}
if (error.ExceptionInfo is BadUsageException)
{
var message = error.ExceptionInfo.Message;
var readTo = message.IndexOf("Class:");
return message.Substring(0, readTo);
}
return string.Format("Line: {0}. An unspecific error occured.", error.LineNumber);
}
public static string BuildMessage(this ConvertException exception)
{
return string.Format("Line: {0}. Column: {1}. Field: {2}. Cannot convert '{3}' to type: '{4}'", exception.LineNumber, exception.ColumnNumber, exception.FieldName, exception.FieldStringValue, exception.FieldType.Name);
}
}
but these extensions still leave a lot to be desired. Is it possible to customize the error messages?
It's hard to improve on your extension methods without it being more hassle than it's worth.
You cannot subclass the default converters (e.g., FileHelpers.ConvertHelpers.Int32Converter since they are internal and sealed). You could create your own custom converter for each type (and base it on the corresponding source code from FileHelpers, e.g., Int32Converter). Then you can raise an alternative to ConvertException (also sealed so you cannot subclass) which would format the message differently.

NHibernate: Could not create the driver from Test.SqlServerCeDriver_ImageFix

Im trying to resolve an issue where when using NHibernate with a SqlServerCeDriver that uses an image column you receive an error: "Byte array truncation to a length of 8000.". I found the following solution:
http://mgeorge-notes.blogspot.com/2009/05/nhibernate-mapping-from-binary-to.html
And created the following class:
namespace Test
{
public class SqlServerCeDriver_ImageFix : SqlServerCeDriver
{
protected override void InitializeParameter(IDbDataParameter dbParam, string name, SqlType sqlType)
{
base.InitializeParameter(dbParam, name, sqlType);
if (sqlType is BinarySqlType)
{
PropertyInfo dbParamSqlDbTypeProperty = dbParam.GetType().GetProperty("SqlDbType");
dbParamSqlDbTypeProperty.SetValue(dbParam, SqlDbType.Image, null);
}
}
}
}
But when I change the NHibernate mapping from
NHibernate.Driver.SqlServerCeDriver
to
Test.SqlServerCeDriver_ImageFix
I get the error, but I am not sure why.
The inner exception is: "Could not load type Test.SqlServerCeDriver. Possible cause: no assembly name specified."
Anyone have any ideas as to what im doing wrong?
When defining the driver in the config, define it with the AssemblyQualifiedName, i.e.:
Test.SqlServerCeDriver_ImageFix, MyAssemblyThatContainsThisType