Category Archives: Software

What is Wrong with JSON Viewer on Codeplex

JSON Viewer for Windows (hosted on Codeplex) is a great tool, I use it at least once a week. However it hasn’t been updated since 2011:
about-json-viewer

Here is the list of issues with version 1.2 of JSON Viewer.

1. When you open a large JSON file (more than 10 MB) the viewer freezes for 2-3 minutes, sometimes longer. You have to kill it with Task Manager.

2. When you edit JSON the cursor always jumps to the beginning of the file – very annoying. Making any changes to JSON is essentially impossible.

3. The URLs in JSON look like hyperlinks but clicking on them does nothing:
links-in-json-viewer

4. When you press Ctrl+V in the search field the text gets pasted to the JSON document instead, usually making it invalid!

Did I miss anything?

How to View Large JSON Files on Windows

Let’s say you need to view a huge (more than 1 GB) JSON file. Of course you are not going to view so much data, it’s humanly impossible. What you want to do is to get a general understanding of JSON structure.

I assume that JSON document is already properly formatted.

First of all we will copy first 10,000 lines to a new file. We will user PowerShell for that:

Get-Content large.json -TotalCount 10000 | Out-File truncated.json

Next, open truncated.json in Sublime Text Editor and scroll to the end:
sublime-truncated-json

The JSON is not valid right now. Let’s make it valid by closing all ‘[‘ and ‘{‘ brackets. Sublime Text has a great feature that highlights invalid JSON code:
sublime-invalid-json-highlighted

All you need to do is to try to add brackets alternating between ‘[‘ and ‘{‘ until you reach the first column:
sublime-valid-json

Now you can save the file and open it in JSON Viewer. Since the new file is small you should have no problems viewing it as tree:
json-viewer

By the way, I’m thinking of building a tool that would automate this process – leave a comment if you’re interested in learning more.

Viewing JSON

From time to time I need to examine data in JSON format. Now unformatted JSON is very hard to read, for example:

{"movies":[{"id":"770739679","title":"Captain America: The First Avenger","year":2011,"mpaa_rating":"PG-13","runtime":121,"critics_consensus":"With plenty of pulpy action, a pleasantly...","release_dates":{"theater":"2011-07-22"},"ratings":{"critics_rating":"Fresh","critics_score":71,"audience_score":96},"synopsis":"Captain America: The First Avenger will focus on the early days...","posters":{"thumbnail":"http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg","profile":"http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg","detailed":"http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg","original":"http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg"},"abridged_cast":[{"name":"Chris Evans","characters":["Captain America/Steve Rogers","Steve Rogers / Captain America","Steve Rogers/Captain America"]},{"name":"Hayley Atwell","characters":["Peggy Carter"]},{"name":"Sebastian Stan","characters":["Bucky Barnes","James Buchanan \"Bucky\" Barnes"]},{"name":"Tommy Lee Jones","characters":["Colonel Chester Phillips"]},{"name":"Hugo Weaving","characters":["Johann Schmidt/Red Skull","Johann Schmidt/The Red Skull","Red Skull"]}],"alternate_ids":{"imdb":"0458339"}}

I usually use JSON Viewer to format JSON:

{
    "movies": [
        {
            "id": "770739679",
            "title": "Captain America: The First Avenger",
            "year": 2011,
            "mpaa_rating": "PG-13",
            "runtime": 121,
            "critics_consensus": "With plenty of pulpy action...",
            "release_dates": {
                "theater": "2011-07-22"
            },
            "ratings": {
                "critics_rating": "Fresh",
                "critics_score": 71,
                "audience_score": 96
            },
            "synopsis": "Captain America: The First Avenger will focus...",
            "posters": {
                "thumbnail": "http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg",
                "profile": "http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg",
                "detailed": "http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg",
                "original": "http://content9.flixster.com/movie/11/15/83/11158339_tmb.jpg"
            },
            "abridged_cast": [
                {
                    "name": "Chris Evans",
                    "characters": [
                        "Captain America/Steve Rogers",
                        "Steve Rogers / Captain America",
                        "Steve Rogers/Captain America"
                    ]
                },
                {
                    "name": "Hayley Atwell",
                    "characters": [
                        "Peggy Carter"
                    ]
                },
                {
                    "name": "Sebastian Stan",
                    "characters": [
                        "Bucky Barnes",
                        "James Buchanan \"Bucky\" Barnes"
                    ]
                },
                {
                    "name": "Tommy Lee Jones",
                    "characters": [
                        "Colonel Chester Phillips"
                    ]
                },
                {
                    "name": "Hugo Weaving",
                    "characters": [
                        "Johann Schmidt/Red Skull",
                        "Johann Schmidt/The Red Skull",
                        "Red Skull"
                    ]
                }
            ],
            "alternate_ids": {
                "imdb": "0458339"
            }
        }
    ]
}

Much better isn't it?

JSON Viewer is great because it also shows JSON in a tree:
JSON Viewer

Now I can easily expand and collapse elements and quickly understand the structure of the data.

However there is one problem: JSON Viewer doesn't handle large files well. If I try to open 4 MB file it just freezes for few minutes and I have to kill it with Process Explorer.

Do you have the same problem too? How would you like to see the problem solved? Please answer as a comment below.

My First Electricity Calculator

I have built my first online electricity calculator. It’s very simple and there are lots of similar calculators out there but it’s a start.

Behold: Electricity Calculator

Here’s how it works. Let’s say you are wondering how much it would cost to run a nightlight if you leave it on all the time. This nightlight has 0.2 watt LED, so you enter 0.2 as wattage.

nightlight

Next you enter 24 as the number of hours per day. Here in Australia we pay 30 cents per kilowatt-hour on average. The calculator gives you the result: the running cost would be $0.53 per year.

Electricity Calculator

Another example. We have 6 light bulbs in our kitchen. They are CFLs (of course) and 14 watt each.

light-bulb

So in total they use 6 * 14 = 84 watt. We have them on for about 3 hours per day. We pay 40 cents per kilowatt-hour. According to the calculator the kitchen lighting cost us $3.02 per month or $36.79 per year.

electricity-cost-lighting

How to Configure Windows 7 to Put Your Computer to Sleep Automatically

I used to leave my computer on all the time – just so that I don’t have to wait for it to start. Then, I upgraded my computer. As part of the upgrade I switched from Windows XP to Windows 7. What I noticed is now my computer goes to sleep automatically when I don’t use it. I think this is great for saving electricity.

Here is how to make sure that Windows 7 puts your computer to sleep automatically.

Click Start button, open Control Panel.
start-menu

Open ‘Hardware and Sound’ category.
control-panel

Open ‘Power Options’.
hardware

Click ‘Change when the computer sleeps’.
power-options

Set ‘Put the computer to sleep’ to 20 minutes.
plan-settings

The default is 30 minutes which is reasonable; 20 minutes is even better. Click Save Settings. Now if you don’t use your computer for 20 minutes it will go to sleep automatically.

While you there click ‘Choose what the power button does’ …
power-options-power-button

… and select ‘Sleep’ option.
power-button

I like to be able to quickly put my computer to sleep when I know that I finished for today. Also, when my kids were small they liked to press the power button just to see what will happen. Usually this resulted in Windows starting to shut down the computer and closing half of the programs before getting to some editor that would ask if I want to save the document before closing it.

The computer uses very little energy in sleep mode but starts up very quickly and in exactly the same state as you leave it. Another cool benefit is that you can wake it up by pressing any key or clicking the mouse. Keep in mind that you still need to shut down your computer if you want to unplug it. If your desktop computer loses power while it is in the sleep mode it would reset its state: all open programs and documents would be abruptly closed. This is why it’s a good idea to save your work when you step away from your computer – just in case of power outage.

See also: Sustainable Computing: Sleep or Hibernate?

Front Page with a Big Image in WordPress

I’m going to continue with setting up my photography website using WordPress and Twenty Twelve theme (see the first post in the series).

As a reminder, this is how the home page should look like:
Home page mockup

By default, WordPress shows latest blog posts on the home page of your site. Here is how to make it to show a single page instead. First, go to WordPress dashboard and click ‘Pages’ in menu on the left:
wordpress-pages-menu

Then, click ‘Add New’ link:
add-new-page-link

Enter ‘Home’ as a title and leave /home as a permalink:
add-new-page

Select ‘Front Page Template’ in page attributes section on the right. Otherwise you will get a sidebar on this page.
page-template

Click ‘Publish’ button. The page is ready, now it’s time to make it the home page. Go to Settings, then Reading:
reading-settings

Change ‘Front page displays’ setting to a static page and select ‘Home’ as front page:
wordpress-front-page

Here’s the result:
front-page-no-image

Our home page is almost ready. Now it’s time to add a big image to it. Go to Pages, then All Pages and click Edit link for the Home page:
all-pages

Click ‘Add Media’ button:
add-media

Switch to Upload tab and select your file. The optimal image width for Twenty Twelve theme is 960 pixels. However you can upload a bigger image – WordPress will resize it automatically. The height doesn’t matter but I like image proportions to be close to the golden ratio.

Once upload is finished set the ‘Link To’ to None and Size to ‘Full Size’ under attachment display settings:
attachment-display-settings

Click ‘Insert into page’ button. The image is in the post but WordPress helpfully made it fit its default column size (625 pixels). Switch to Text tab and remove width and height attributes from the img tag:
img-width-height

Now set template to ‘Full-width Page Template, No Sidebar’ and click Update button.

Voilà, home page in WordPress with a full-width image:
home-page-with-full-width-image

You can view result live: Acme Photography.

How to Set Up a Photography Website Using WordPress

Let’s say I’m a professional photographer and I need a new website. The best thing to do generally is to ask someone else to do it. But what if I’m just starting out and don’t have money to spend on a fancy website? I can get a basic website for free using WordPress.

First of all, I’m going to create a mockup of my new website:

Home page mockup

Home page should have a nice big image, a heading and a menu bar. The gallery page should have thumbnails. Visitor should be able to click on any thumbnail to view a bigger photo. Something like this:

Gallery page mockup

Looks good! Next, I need to decide where I’m going to host my website. WordPress.com looks like a good option: it’s free (as long as your website address ends with .wordpress.com) and it is maintained by the same people that make WordPress.

Go to www.wordpress.com and sign up for a new account.

Creating new website at WordPress.com

The address of my website is going to be acmephotography.wordpress.com

Sign up form at WordPress.com

This is how my brand new website looks like:

Brand new WordPress website

Not very close to the mockup, isn’t it? Let’s fix it.

The next step is to choose a theme for my website. The problem with WordPress themes is that there are just too many of them. After some poking around I decided to try Twenty Twelve theme by the WordPress team. It looks nice and crisp. It has a menu on top. Also, it’s free! Here’s how to set it up.

Go to ‘My Blog’ on WordPress.com and click ‘Dashboard’ link there.

My blog - with Dashboard link

Move mouse over ‘Appearances’ link in the menu on left, then select ‘Themes’:

wordpress-themes-menu

Search for ‘Twenty Twelve’ theme and click Activate button:

Activating WordPress theme

Close the dialog confirming that the theme has been activated. Go to Settings, then General. Set site title to ‘Acme Photography’. Remove tagline (set it to empty):

General settings in WordPress

Don’t forget to click ‘Save Changes’ button. Now click on website name on the top left to view your website.

View website link in WordPress dashboard

Behold: my new photography website, Acme Photography:

Website with Twenty Twelve WordPress template

It has a title and a menu bar. However the front page doesn’t look right yet. I explain how to configure front page in my next post.

Command History in Far Manager

Far Manager has powerful suppport for command history. ConEmu author even suggests using Far Manager with panels turned off instead of standard command prompt.

Ctrl+E gives you previous command entered. You can press it multiple times to get older entries. Ctrl+X moves you forward in the history.

Press Alt+F8 to view command history in the list:

You can execute a command from the list by pressing Enter. If you want to edit a command press Ctrl+Enter.

All shortcuts available in command history list:

Enter Re-execute a command
Shift+Enter Re-execute a command in a new window
Ctrl+Alt+Enter Re-execute a command as administrator
Ctrl+Enter Copy a command to the command line
Del Clear the commands history
Ins Lock/unlock a history item
Shift+Del Delete the current history item
Ctrl+C or Ctrl+Ins Copy the text of the current command to clipboard

Another useful shortcut is Ctrl+Alt+F – it lets you to search in the list by filtering it down. This shortcut is totally non-obvious and not documented anywhere so I was happy when I discovered it. It also works in folder history (Alt+F12).

Far Manager and Console Output

One of the strongest features of Far Manager is the ability to start any program or script from command prompt. You can type anything there, just like in standard command prompt. Ctrl+Enter shortcut greatly helps here – it inserts selected file name to command prompt (Ctrl+F inserts full path).

If you launch a console program all program output will be displayed in Far window. Terrific! After program finishes you can view console output by pressing Ctrl+O.

There is a problem however: you can’t scroll up if program output is long. Only last 25 or so lines of text are visible. Anything before that is lost forever.

Far has a clever little known trick to view all program output. Add view:< before the command and all console output will be intercepted and redirected to internal viewer. For example:

view:<ipconfig /all

You can even redirect output to Far editor by using

edit:<ipconfig /all

This is nice but not ideal. You have to remember to add view:< before starting a console program or script. If a program takes long time to execute you won’t see any progress until it finishes. Also any interactive prompt in program screws the whole process.

ConEmu to the Rescue

ConEmu is a console emulator. It has lots of great enhancements to standard Windows command prompt. It works by intercepting all console output and displaying it in its own window.

ConEmu is also a close friend with Far Manager. After installing ConEmu you can open Far Manager inside ConEmu window:

Far Manager in ConEmu

Once Far Manager is inside ConEmu tab you can scroll console output by switching to so called alternative mode.

This mode hides Far panels and ‘freezes’ console. Ctrl+Up scrolls up, Ctrl+Down scrolls down. Scrolling with a mouse wheel also works.

But that’s not all. You can also assign Ctrl+O to view console output in Far viewer. To do this locate CtrlO_View.reg in ConEmu folder and execute it.

Link

Scott Hanselman on ConEmu

PowerShell Exit Codes

Recently I came across an interesting quirk in PowerShell: exit codes from powershell.exe are inconsistent.

As you may know there are two ways of launching PowerShell script: you can use -File or -Command parameter.

powershell -File test.ps1

or

powershell -Command .\test.ps1

-Command could be ommitted:

powershell .\test.ps1

Now let’s say you want to execute PowerShell script from a scheduled task or from continuous integration server. Naturally, you would be interested if the script fails or not.

And here’s the bug: if you launch your script using -File parameter powershell.exe exits with zero code even if there was an exception.

You can confirm this by creating a script that always throws an error:

throw 'oops'

Now if you launch it using -File parameter exit code is 0:

powershell.exe -File testerror.ps1
echo Error level is %ERRORLEVEL%

oops
At C:\testerror.ps1:1 char:6
+ throw <<<<  'oops'
    + CategoryInfo          : OperationStopped: (oops:String) [], RuntimeException
    + FullyQualifiedErrorId : oops

Error level is 0

But if you use -Command parameter exit code is set as expected:

powershell.exe -Command .\testerror.ps1
echo Error level is %ERRORLEVEL%

Chris Oldwood also came across this bug:

depending on whether I use the “-File” or “-Command” switch to execute the .ps1 script I get different behaviour. Is this a PowerShell bug or is there something fundamental about the execution model the differs between -File and -Command that I’ve yet to understand?

Bonus

How to execute PowerShell script from NAnt script:

<exec program="powershell.exe" append="true">
  <arg value="-noprofile" />
  <arg value="&amp;'C:\your-script.ps1'" />
  <arg value="param1" />
  <arg value="'param 2 with spaces'" />
</exec>

References