Hello wise perl monks,

I am hoping someone here can point me in the right direction. I have a message processing system written in perl using JSON as the primary transport encoding. The main process receives STOMP messages using AnyEvent::STOMP. Those messages are encoded in json by php and received by perl and decoded. The messages are JSONRPC messages with a "method" and "params". When I decode them, I then re-encode the params and use a AnyEvent::JSONRPC::TCP::Client to send them to the AnyEvent::JSONRPC::TCP::Servers listening on unix sockets. This is a service I have been running for 2+ years, and the bit of code responsible for processing the stomp messages has remained unchanged for most of the life of the project. I am running perl-5.18 in a Docker container based on ubuntu:14.04.

I recently flushed all of my layer caches and built the entire container from scratch, and I am now having a problem with the JSON encoding which has me frazzled. I've spent 8+ hours trying to figure out specifically what might have changed, but I'm coming up with nothing. In my stomp client process I have always used JSON->new->allow_nonref as my encoder because some of the "params" that come through are an array once decoded and some are not. Using allow_nonref made this something I could handle without having to change anything based on how the message was received. However, after building the new image I found the following exception messages in my log, and the messages are not making it to the JSONRPC::Server any longer.

unhandled callback exception on event (MESSAGE, AnyEvent::STOMP::Client=HASH(0x7432b18), HASH(0x1a9da520) {"method":"echo","params":[{"Deployment":{"id":"57276a74-8790-48aa-b7d6-42480a6e0303","status":268435970,"comment":"marathon| Creating new application for website 1088 in marathon","website_id":1088,"district":"devel"}}]}): hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) at /usr/local/lib/perl/5.18.2/AnyEvent/Handle.pm line 1135

The AnyEvent::Handle module line 1135 is using the encode method of JSON::XS->new->utf8. I tried to reproduce this issue by using the same encode method on the same data via perl cli and I am unable. I modified the module code to have it dump the data it was receiving as it's $ref input, and it is as I suspected a valid hashref. I used that dumped hashref, I tried changing from allow_nonref to encode/decode it in different ways before sending it to the client, but nothing I do will reproduce the issue in my tests.

use JSON::XS; use Data::Dumper; my $json = JSON::XS->new->utf8; my $input = '{"method":"echo","params":[{"Deployment":{"id":"57276a74- +8790-48aa-b7d6-42480a6e0303","status":268435970,"comment":"marathon| +Creating new application for website 1088 in marathon","website_id":1 +088,"district":"devel"}}]}'; print Dumper { input => $input, output => $json->decode($input), re_en +coded => $json->encode($json->decode($input)) }; $VAR1 = { 'input' => '{"method":"echo","params":[{"Deployment":{"id":" +57276a74-8790-48aa-b7d6-42480a6e0303","status":268435970,"comment":"m +arathon| Creating new application for website 1088 in marathon","webs +ite_id":1088,"district":"devel"}}]}', 're_encoded' => '{"params":[{"Deployment":{"status":26843597 +0,"district":"devel","comment":"marathon| Creating new application fo +r website 1088 in marathon","id":"57276a74-8790-48aa-b7d6-42480a6e030 +3","website_id":1088}}],"method":"echo"}', 'output' => { 'params' => [ { 'Deployment' => { 'id' => '572 +76a74-8790-48aa-b7d6-42480a6e0303', 'website_id' + => 1088, 'comment' => + 'marathon| Creating new application for website 1088 in marathon', 'district' = +> 'devel', 'status' => +268435970 } } ], 'method' => 'echo' } };

In reply to JSON encoding issues by sedninja

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.