JSON and decimal values

In this short post I will explain why floating point or large decimal values should be represented as strings when transferring data using JSON. This post is based on my recent discovery when trying to fix a nasty bug, when running my app on iOS 11 beta3.

Let’s assume that we have a simple JSON representation, containing a large decimal value. Let it be 79228162514264337593543950335, which is Decimal.MaxValue (in C#)

"data": 79228162514264337593543950335

If you want to parse this on iOS 11 beta 3 you will get:

which surprisingly returns a rounded result:


When you send this again to some C# backend, you will probably get a parsing error as this number is larger than Decimal.MaxValue.

What may surprise you, this works perfectly on iOS 10:

It looks like between iOS 10 and 11, JSONSerialization, under the hood, started to use directly NSNumber instead of NSDecimalNumberPlaceholder. NSNumber cannot represent such a big number and the result is not correct.

Hence, if you want to pass a large number in JSON or if precision is of importance, values should be passed as strings.

AFAIK, in JSON numeric precision is not specified and each parser may choose whatever is convenient. If precision is important you should always pass numeric values as strings and convert them to appropriate representation in your client application.

If you enjoyed this short post, please follow me on twitter: @tgebarowski

Many thanks to Pawel Kowalczuk @riamf1, for his contribution to this post.

Closures argument + member function = retain cycle?

After introducing ARC memory management was simplified a lot, but still could not be forgotten. For sure every developer working with either Objective-C or Swift had to deal with retain cycles. There are lots of in depth posts about this topic and I do not want to rephrase them, so instead I will link to my favourite from krakendev. Unfortunately this post does not cover everything and I would like to mention some specific case which I encountered not that long ago.

Imagine following example, where private member function is passed as a closure argument:

The problem with the above code is that sendRequest returns its result using @escaping closure from a background queue.
Passing a private handleResponse function as a closure argument creates a retain cycle, hence deinit is never called on Dispatcher object.

The problem could be easily solved by either:

  • inlineing handleResponse content withing the closure where self is weak/unowned
  • wrapping handleResponse with another closure where self is weak/unowned

The above code looks ugly and is less compact. What if we created a helper function allowing us to wrap up this code?

and then use the following code?

Not perfect, but definitely more compact. Anyway, I think that this is something that could be addressed in new version of Swift.

Check out my GitHub project with Swift One-Liners, where you can find this exemplary wrapper, extended with versions having different closure signatures.

If you enjoyed this article, please follow me on twitter: @tgebarowski

Dealing with a static cling in Swift

As programmers we often have to deal with legacy code, usually not written by us, sometimes of bad quality, without unit tests, hard to modify, with high regression risk. One of ubiquitous code smells is so called static cling. It is so frequent because lots of developers “love” static functions, singletons and in fact greatly abuse those patterns.
We do so, because static code seems easier and faster to write, but in a longer run, the code written using those patterns is hard to test, not reentrant, coupled, maintaining global state and is just badly designed.

Read More

Taming Swift compiler bugs

Xcode 7.2 has a bug which results in compilation failure of project having +1500 Swift files. It seems that swiftc is buggy, but you can try to bypass that behaviour by using my wrapper script. Read the whole post if you are interested in what happens under the hood.

Read More


For the last two weeks I’ve been busy doing my side project, a library helping building in-app settings for iPad and iPhone apps.

By providing a declarative DSL, SwiftySettings allows to define your own settings, integrate with storage interface and automatically generate native Settings app like user interface.

Read More

Ubuntu 11.4 installation from USB on MacBook Air

Installing Ubuntu on a MacBook Air which has no CD drive is a tricky task. First of all it is impossible to boot from the USB stick created in Ubuntu, secondly even if you manage to boot from it, the installer will fail as it tries to read from /cdrom anyway.

Some time ago I found the following thread which described step by step procedure of generating bootable USB stick, recognized as CD drive. In order to make it working, you have to download and extract the mkhybridiso.tar.gz (from the above page) and remaster your Ubuntu ISO with the following command:

tar xvf mkhybridiso.tar.gz
cd mkhybridiso
sudo ./mkhybridiso.sh /PATH/your-ubuntu.iso

After some time a hybrid ISO is generated (with ‘hybrid’ suffix), when this is done, copy the ISO onto USB stick:

sudo dd if=your-ubuntu_hybrid.iso of=/dev/sdb bs=1M

Note that /dev/sdb corresponds to your USB drive identifier (you can get the proper name by executing dmesg | less just after inserting the USB stick).
When USB stick is ready, you can reboot the MBA and start the installer (for example via rEFIt). It is very probable that the installation hangs on a black screen. In order to avoid that make sure that you add “nomodeset” parameter to the initializer list, just before invoking the installer from the purple menu. If all steps are accomplished the installation should end with success.

Unfortunately, the above procedure doesn’t work well when we have already installed Ubuntu (hence GRUB as well) and we want to boot again from the same Ubuntu USB installer. It doesn’t matter how many times the Mac is restarted you will always end up with rEFIt executing GRUB and Ubuntu from the HD.
In this specific situation, in order to start up from the USB stick installer you may try invoking GRUB command line (by pressing ‘c’ i the GRUB menu).
When GRUB prompt starts, try executing the following:

root (hd1)
set gfxpayload=keep
linux /casper/vmlinuz file=/cdrom/preseed/ubuntu.seed boot=casper only-ubiquity nomodeset --
initrd /casper/initrd.lz

If the sequence is executed correctly, the Ubuntu installer should start booting.

GList iterate and remove pattern

When I checked my blog’s Google Analytics stats, I noticed that someone looked for a way to iterate over a GList and remove its elements at the same time.
This is quite a common problem assuming that you deal with some objects appended to the list. The object can have some expiration timeout or can be marked for lazy removal. An independent process can loop through the list from time to time and remove those marked for releasing. This simple code should do the thing:

/* Set the iterator to the beginning of our list */
GList *node_itr = priv->_list;
while (node_itr != NULL)
   GObject *obj = (GObject *)node_itr->data;
   /* Store next element's pointer before removing it */
   GList *next = g_list_next(node_itr);
   /* Some dummy removal condition */
   if (my_gobject_marked_for_removal(obj))
      priv->_list = g_list_delete_link(priv->_list, node_itr);
   node_itr = next;

Emacs Elisp macro for generating GObject from template

Everyone who has worked with GObjects knows how time consuming is to create a new GObject. In order to make it working you have to generate lot of boilerplate code which is more or less the same for all the objects.
In most of the cases people tend to use templates and make several find-replace substitutions to generate the output GObject.

To simplify everything and to learn more about Elisp I decided to build simple Elisp macros for generating GObjects directly from the Emacs command line. The script prompts for some basic info, like name of the final object (ex. MyExtremelyUselessObject), the output directory where the object is to be generated and some brief description of the object (for Doxygen purpose). While preparing templates I tried to follow GObjects name conventions and used Doxygen for documenting the source code (I know that probably GTK-Doc would be better but in all my current projects I use Doxygen so I know it better). The the set of macros can be downloaded from here: emacs-gobject-macros.tar.gz , a detailed installation instruction is inside the Elisp script. Note, that this script can also generate some kind of C-Objects based on normal C-struct’s.
If you happen to make some useful modification to the script, please let me know!

My first Elisp script

It’s already been one year since I switched from Vim to Emacs (thanks Aleksander;) I heard a lot about how powerful the Elisp is, but I didn’t have much motivation in learning it. I started playing with Elisp last weekend and I must say that is much different from everything I used before. Anyway because I work a lot with GObjects and Glib I decided to create some useful script which will speed up my coding. My first script is trivial but it helps to create GList iterator, it acts as a simple code snippet with this difference, that the initial position of the iterator is assigned with the identifier under the cursor.

Elisp script:

(defun azt-glist-iterate-all()
    "Creates a GList iterator for
      the list under the cursor"
    (setq itr_template "GList *node_itr = vvv;
while (node_itr != NULL)
    /* GType *data = (GType *)node_itr->data; */
    node_itr = g_list_next(node_itr);
    ; Replace patter with a word at point
    (setq initializer
             (replace-regexp-in-string "\*" "" 
                   (thing-at-point 'symbol)))
    (setq itr_template 
             (replace-regexp-in-string "vvv" 
                     initializer itr_template))
    (forward-line 1)
    (insert itr_template)

When the script is loaded into Emacs and you start typing something like “GList *my_list = priv->_list” , put your cursor on my_list identifier and M-x azt-glist-iterate-all you will generate the snipper below:

GList *node_itr = my_list;
while (node_itr != NULL)
    /* GType *data = (GType *)node_itr->data; */
    node_itr = g_list_next(node_itr);

Mounting ext3 partition on OS X 10.6 (Snow Leopard)

Recently I’ve faced some problems with mounting a large (~ 850 GB) ext3 partition on OS X 10.6. I didn’t except any difficulties as I have already mounted ext2 or 3 partitions on my MBP running Leopard. Normally it could be easily done with Macfuse and fuse-ext2 extension. Unfortunately in this specific case it simply didn’t work. I have realized that I’m running a 64-bit version of Snow Leopard and Macfuse is only officially delivered for 32-bit architecture. This explained a lot! I managed to find an unofficial 64-bit compilation of Macfuse which could be downloaded from Tomas Carnecky blog. I installed the 64-bit version and used the old fuse-ext2 plugin, unfortunately when trying to mount the ext3 partition I got the following error:

$fuse-ext2 /dev/disk2s2 /Volumes/Private/

fuse-ext2 /dev/disk2s2 /Volumes/Private/fuse-ext2: version:’0.0.7′, fuse_version:’27’ [main (../../fuse-ext2/fuse-ext2.c:324)]
fuse-ext2: enter [do_probe (../../fuse-ext2/do_probe.c:30)]
fuse-ext2: Error while trying to open /dev/disk2s2 (rc=16) [do_probe (../../fuse-ext2/do_probe.c:34)]
fuse-ext2: Probe failed [main (../../fuse-ext2/fuse-ext2.c:340)]

After googling a little bit, I discovered that this error could be caused by some conflict with previously installed ext2fsx. During my small fight with Snow Leopard and ext3 I tried several possibilities and I totally forgot about them. It seemed that ex2fsx might have caused some conflicts with Macfuse. After uninstalling the ext2fsx the drive could be successfully mounted with the command mentioned above.
FYI, the ext2fsx can be easily removed by using uninstallation script delivered in the dmg package. Also when you mount the device make sure that you specify it correctly by referencing to corresponding /dev/diskXXX. The exact name of the device can be read from the Disk Utility application (right clicking on the partition and selecting “Information”)

Note, that the name of fuse-ext2 may suggest that only ext2 partitions are supported, fortunately both ext3 and ext4 partitions should work correctly as well.


GRepoApplet is a simple GNOME Panel applet for monitoring status of Version Control Systems. Currently only SVN repositories are supported but due to provided backend API it should not be difficult to support more types of VCS’s or even DVCS’s. The applet allows to specify arbitrary number of monitored repositories and time interval between each query. All the recent changes are displayed in a nicely looking notification box generate by the libnotify.

The applet is written in C and is using Glib and GObject libraries. For better integration with GNOME and storing user configuration GConf and GnomeKeyring are used.

Read More

Boost ptr_map iterator incompatibility

Boost library provides a very powerful set of data structures and algorithms which are intended to be cross platform and rather concise. Unfortunately it happens, that the API of the library changes from version to version. The problem which I faced recently was related to ptr_map container iterator and different methods allowing for accessing keys and values of the container. In boost <=1.33 the mentioned fields could be accessed via iterator->key() and (*iterator) methods respectively. Starting from version 1.34 the authors of the library introduced approach which is compatible with map container from STL and uses iterator->first and iterator->second fields.

Unfortunately I was forced to use different platforms for development (Ubuntu 9.04 – Boost 1.39) and deploying (CentOS 5.3 with Boost 1.33), that’s why some problems appeared.
In order to solve them I used some conditional compilation tricks and some basic AutoConf routines.

First of all in configure.ac file I added the following condition, which tries to compile a given instruction using ptr_map iterator from Boost > 1.33.
If it succeeds the HAVE_PTR_MAP_ITR_SECOND value is defined in config.h file and we are sure that the platform supports STL compatible method of accessing map entries.

[#include ],
[boost::ptr_map::iterator itt;int *i=itt->second;],
[ptr_map::iterator defines second field]))

Later I created a short macro which will be replaced with appropriate code depending on the HAVE_PTR_MAP_ITR_SECOND condition:

#define ptr_map_itr_second(s) ((s)->second)
#define ptr_map_itr_second(s) (&(*s))

The exemplary code showing application of the created macro can be found below:

ptr_map::iterator it;
it = ports_map_.find("key");

if (it != ports_map_.end())
  OffPortRange *port_range =  ptr_map_itr_second(it);

Sindbad PV CAD

Sindbad PV is a complex CAD (computer-aided design) application for designing photovoltaic installations for houses taking into account the surrounding obstacles and providing the power balance and cost estimation.

Read More