Error deserializing object of type int

hello , I am having this error when I am declaring a nullable integer with attribute data member and then trying to send an object of type x from jquery
  • Remove From My Forums
  • Question

  • hello , I am having this error when I am declaring a nullable integer with attribute data member and then trying to send an object of type x from jquery

    There was an error deserializing the object of type x. The value » cannot be parsed as the type ‘Int32′.’. 

    the idea is that my object has a field of NULLABLE integer , so when it’s sent with a value it’s working , but if it’s an empty string then it gives me an error , what to do?

Answers

  • May be this is a problem with your JQUERY Code … 

    I have the following which works fine 

    DataContract 

    namespace TestWcfService

    {

        [DataContract]

        public class NullTester

        {

            [DataMember]

            public int? Val;

        }

    }

    Service Interface 

    namespace TestWcfService

    {

        [ServiceContract]

        public interface IMyService

        {

            [OperationContract]

            NullTester GetNullTesterObj(int? i); 

        }

    }

    Service imp

     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

        public class MyService : IMyService

        {

            public NullTester GetNullTesterObj(int? i){

                NullTester nt = new NullTester();

                if (i != null) 

                {

                    nt.Val = i;

                }

                return nt; 

            }

    }

    Client 

    ====== 

            TestService.MyServiceClient ts = new TestService.MyServiceClient();

                 bool val = (ts.GetNullTesterObj(null).Val == null);

                 Console.WriteLine(«Val:{0}», val.ToString()); // prints true

                 val = (ts.GetNullTesterObj(5).Val == null);

                 Console.WriteLine(«Val:{0}», val.ToString()); //prints false 


    Tanvir Huda

    • Marked as answer by

      Friday, March 4, 2011 2:02 PM

Recommend Projects

  • React photo

    React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo

    Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo

    Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo

    TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo

    Django

    The Web framework for perfectionists with deadlines.

  • Laravel photo

    Laravel

    A PHP framework for web artisans

  • D3 photo

    D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Visualization

    Some thing interesting about visualization, use data art

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo

    Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo

    Microsoft

    Open source projects and samples from Microsoft.

  • Google photo

    Google

    Google ❤️ Open Source for everyone.

  • Alibaba photo

    Alibaba

    Alibaba Open Source for everyone

  • D3 photo

    D3

    Data-Driven Documents codes.

  • Tencent photo

    Tencent

    China tencent open source team.

I am trying to put my own Serializable objects into a Topic.

I have a message driven bean listening on this and in the method onMessage, i try to cast the object received to the Serializable object sent.

I get the following error

weblogic.jms.common.JMSException: Error deserializing object
at weblogic.jms.common.ObjectMessageImpl.getObject(ObjectMessageImpl.jav
a:144)
at DbLoggerBean.doLogging(DbLoggerBean.java:71)
at LoggerBean_8hiuxq_EOImpl.doLogging(LoggerBean_8hiuxq_EOImpl.java:45)
at LoggerBean_8hiuxq_EOImpl_CBV.doLogging(Unknown Source)
at MessageListenerBean.onMessage(MessageListenerBean.java:54)
at weblogic.ejb20.internal.MDListener.execute(MDListener.java:382)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.spi.LoggingEvent
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClass
Loader.java:198)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at weblogic.utils.classloaders.GenericClassLoader.loadClass(GenericClass
Loader.java:223)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at weblogic.jms.common.ObjectMessageImpl$ObjectInputStream2.resolveClass
(ObjectMessageImpl.java:305)

In my client program that publishes messages to a topic, the code looks like this

class Result implements Serializable {
….
….
}

Result result = new Result();
result.setXXX();
……..
logger.info( rslt);

I use the JMSAppender provided by the Log4j. So, I donot have a reference to a Topic object and neither can i invoke topic.createObjectMesage…..

In my MDB, I write like this…

if (message instanceof ObjectMessage)
{
ObjectMessage m = (ObjectMessage) message;
Result rslt = (Result) m.getObject();
/* looks like the m.getObject returns a org.apache.log4j.spi.LoggingEvent
and it has a getMessage() method …but it returns a string … and i get a classcastexception
*/
}

I get the Error deserializing object error at the line where i try to getObject() abd cast it to Result.

I use WebLogic 8.1.

Any help in this regard is appreciated.

InvalidFormatException is subclass of MismatchedInputException. InvalidFormatException occurred because of bad formatting or format not match with the presepecified format of value to deserialize.

The issue is passing type is String while deserialization expecting as Int.

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

public class StudentDetail {
private int rollNumber;
private String firstName;
private String lastName;

//getter and setter of class
}
import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJacksonExample2 {

public static void main(String[] args) {

String json="{"rollNumber":"C21" , "firstName":"Saurabh" , "lastName":"Gupta"}";

System.out.println(json);
try
{
ObjectMapper mapper = new ObjectMapper();
StudentDetail student= mapper.readValue(json, StudentDetail.class);
System.out.println(student);
}
catch(IOException ex)
{
ex.printStackTrace();
}
catch(Exception ex)
{
ex.printStackTrace();
}

}

}

InvalidFormatException Stacktrace


{"rollNumber":"C21" , "firstName":"Saurabh" , "lastName":"Gupta"}
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String "C21": not a valid Integer value
 at [Source: (String)"{"rollNumber":"C21" , "firstName":"Saurabh" , "lastName":"Gupta"}"; line: 1, column: 15] (through reference chain: com.fiot.json.jackson.exceptions.StudentDetail["rollNumber"])
    at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1549)
    at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:911)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer._parseInteger(NumberDeserializers.java:522)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:474)
    at com.fasterxml.jackson.databind.deser.std.NumberDeserializers$IntegerDeserializer.deserialize(NumberDeserializers.java:452)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.fiot.json.jackson.exceptions.TestJacksonExample2.main(TestJacksonExample2.java:18)

Solutions

Pass value as the compatible type, here in this case number value roll number can’t accept as a text string with an alphanumeric.

In such cases, if your organization is using rollNumber as alphanumeric then convert int to String to make compatible. or ask your client application to pass the value of rollNumber as an int value.

References

https://fasterxml.github.io/jackson-databind/javadoc/2.9/com/fasterxml/jackson/databind/exc/InvalidFormatException.html

You would like to see

Follow below link to learn more on JSON and  JSON issues solutions:

  • JSON Tutorial
  • JSON Issues Solutions

Понравилась статья? Поделить с друзьями:
  • Error description the software licensing service reported that the product sku is not found
  • Error description the software licensing service reported that the product key is invalid
  • Error description the software licensing service reported that the license is not installed перевод
  • Error description the software licensing service reported that license consumption failed
  • Error description the activation server determined the specified product key has been blocked