Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

from json to custom class using Gson  RSS feed

 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to send JSON object from html file and receive it in servlet and then convert it into custom class of LastMove using

new Gson().fromJson(input, LastMove.class);

Please tell me how to do it?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waqas imtiaz wrote:
I want to send JSON object from html file and receive it in servlet and then convert it into custom class of LastMove using
Please tell me how to do it?

Please define 'send JSON object from html file'. HTML is just the view. How is the JSON object related to it?

Recommended reading: https://sites.google.com/site/gson/gson-user-guide
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here "lastMove" will be sent over the wire will be converted as "LastMove.class" type object
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Please define 'send JSON object from html file'. HTML is just the view. How is the JSON object related to it?

Please TellTheDetails on exactly what you are trying to do
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:
Maneesh Godbole wrote:
Please define 'send JSON object from html file'. HTML is just the view. How is the JSON object related to it?

Please TellTheDetails on exactly what you are trying to do



I am trying to send data to the server side using Json object. This data will be converted to the custom class using Gson library call then it will be used there in servlet.
This is actually a chess game whose front end is implemented in javascript and html css. and back end in javaservlets. Now this is a last move object (the move played). This information is sent to server side servlets and will be used as java object of type class LastMove given below:

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So essentially you want to pass the stringLastMove to the servlet where it would eventually be converted to a JSONObject and processed? If yes check out http://stackoverflow.com/questions/5338943/read-json-string-in-servlet
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:So essentially you want to pass the stringLastMove to the servlet where it would eventually be converted to a JSONObject and processed? If yes check out http://stackoverflow.com/questions/5338943/read-json-string-in-servlet


Actually I am sending "lastMove" which is a JSON object at html side, whereas "lastMoveString" is a string.
What actually happens is that this "lastMoveString" is converted into "lastMove" object and then this object is processed. When process is completed it is transferred to the server side.
At server side this object will be converted/translated into custom class object (not json object).
So please help me on how to convert this JSON object at server side into custom class "LastMove.class" type which is shown above.
Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
waqas imtiaz wrote:
So please help me on how to convert this JSON object at server side into custom class "LastMove.class" type which is shown above.
Thanks

Did you check the link I had shared earlier? That document shows you how to do it

 
Dave Tolls
Rancher
Posts: 2914
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you succeeded in doing?
I don't know about Maneesh, but I'm not entirely clear as to where you are stuck?
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the link I had shared earlier? That document shows you how to do it



I checked it. At client side I did following:



At server side when I do following:



I get following in the console:

INFO: null




And if I stringify above ajax reqeust like following:



I get the following at server side:



and console write the following:

INFO: {"simpleMove":{"pieceMoved":{"name":""},"previousPosition":{"row":"","column":""},"currentPosition":{"row":"","column":""},"nativeNextPossiblePositions":{},"opponentNextPossiblePositions":{},"nativeReturnSet":false,"opponentReturnSet":false},"pieceBeaten":{"pieceBeaten":{"name":""},"beatenBy":{"name":""},"beatingPiecePreviousPosition":{"row":"","column":""},"beatingPieceCurrentPosition":{"row":"","column":""},"beatenPiecePosition":{"row":"","column":""},"nativeNextPossiblePositions":{},"opponentNextPossiblePositions":{},"nativeReturnSet":false,"opponentReturnSet":false},"specialMove":{"eNPassantPlayed":false,"cAstle":false,"pAwnUpgrade":false,"eNPassantGiven":false,"pawnUpgrade":{"pawnName":{"name":""},"upgradedPiece":{"name":""},"pawnPreviousPosition":{"row":"","column":""},"pawnCurrentPosition":{"row":"","column":""},"upgradedPiecePosition":{"row":"","column":""}},"castle":{"kingName":{"name":""},"rockName":{"name":""},"kingPreviousPosition":{"row":"","column":""},"kingCurrentPosition":{"row":"","column":""},"rockPreviousPosition":{"row":"","column":""},"rockCurrentPosition":{"row":"","column":""},"rightCastle":false,"leftCastle":false},"enPassantPlayed":{"beatenPawnName":{"name":""},"beatingPawnName":{"name":""},"beatenPawnPosition":{"row":"","column":""},"beatingPawnPreviousPosition":{"row":"","column":""},"beatingPawnCurrentPosition":{"row":"","column":""}},"enPassantGiven":{"nativePlayer":{"userId":""},"opponentPlayer":{"userId":""},"nativeEnPassantPiece":{"name":""},"nativePiecePreviousPosition":{"row":"","column":""},"nativePieceCurrentPosition":{"row":"","column":""},"rightOpponentEnPassantPiece":false,"leftOpponentEnPassantPiece":false,"opponentRightEnPassantPiece":{"name":""},"opponentLeftEnPassantPiece":{"name":""},"opponentRightPiecePreviousPosition":{"row":"","column":""},"opponentRightPieceNextPossiblePosition":{"row":"","column":""},"opponentLeftPiecePreviousPosition":{"row":"","column":""},"opponentLeftPieceNextPossiblePosition":{"row":"","column":""},"enPassantExpired":true,"nativeNextPossiblePositions":{},"opponentNextPossiblePositions":{},"nativeReturnSet":false,"opponentReturnSet":false},"nativeNextPossiblePositions":{},"opponentNextPossiblePositions":{},"nativeReturnSet":false,"opponentReturnSet":false},"check":{"kingUnderCheck":{"name":""},"checkGivenBy":[null,null],"kingPosition":{"row":"","column":""},"checkGivenPosition":[null,null],"opponentKingNextPossiblePosition":{},"allowedPiecesOfOpponentWithAllowedPositions":{}},"checkMate":{"checkMateBy":{"userId":""},"checkMateTo":{"userId":""}},"sImpleMove":false,"pIeceBeaten":false,"sPecialMove":false,"cHeck":false,"cHeckMate":false,"playedBy":"five12","toBeRecievedBy":"five1","played":true,"recieved":false,"moveSkipped":true,"hostBlockedPieces":false,"clientBlockedPieces":false}


and when I do at server side the following:



I get following in the console :

WARNING: StandardWrapperValve[PlayerMove]: Servlet.service() for servlet PlayerMove threw exception
java.lang.NullPointerException
at ChessBoard.PlayerMove.processRequest(PlayerMove.java:72)
at ChessBoard.PlayerMove.doPost(PlayerMove.java:216)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)

WARNING: StandardWrapperValve[PlayerMove]: Servlet.service() for servlet PlayerMove threw exception
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2663
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at ChessBoard.PlayerMove.processRequest(PlayerMove.java:72)
at ChessBoard.PlayerMove.doPost(PlayerMove.java:216)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2663
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
... 37 more




Now please help on this.
thanks
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:What have you succeeded in doing?
I don't know about Maneesh, but I'm not entirely clear as to where you are stuck?


Please read my last answer it will explain you all.
Thanks
 
Dave Tolls
Rancher
Posts: 2914
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
Sticking that JSON string into a viewer and this part is your problem:

(There may be others, but that stands out to me).

These two are Player attributes in your LastMove class, so Gson is expecting these to be objects and not simple strings, hence the error:
Expected BEGIN_OBJECT but was STRING
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Thanks.
Sticking that JSON string into a viewer and this part is your problem:

(There may be others, but that stands out to me).

These two are Player attributes in your LastMove class, so Gson is expecting these to be objects and not simple strings, hence the error:
Expected BEGIN_OBJECT but was STRING


In StringLastMove it is not a string it is an object:



Then what else could be the problem and how to fix this?
 
Dave Tolls
Rancher
Posts: 2914
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, then something in your Javascript is changing that from an object into a String before it gets sent to the server.
You say earlier:
"What actually happens is that this "lastMoveString" is converted into "lastMove" object and then this object is processed. When process is completed it is transferred to the server side. "

I'm going to guess that something in the highlighted part is changing the value. In fact. it must be as the lastMoveString does not have a value in there apart from the empty userId.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you used the browser tools to look at the actual request? If not, you are flying blind. Use the tools available to you instead of guessing.
 
waqas imtiaz
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Well, then something in your Javascript is changing that from an object into a String before it gets sent to the server.
You say earlier:
"What actually happens is that this "lastMoveString" is converted into "lastMove" object and then this object is processed. When process is completed it is transferred to the server side. "

I'm going to guess that something in the highlighted part is changing the value. In fact. it must be as the lastMoveString does not have a value in there apart from the empty userId.


You are right some data manipulation tempered the class. Now its working.

Thanks. Thanks again to you and every body else as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!