Observe that, $a,b$ must be of same parity , else $a^2+b^2$ will be odd.
$=>n^2=(\frac{a+b}{2})^2+(\frac{a-b}{2})^2=p^2+q^2$ for some integer $p,q$
( So, $a+b=±2p$ and $a-b=±2q$ leading to two pairs of $(a,b)$. )
For $n^2$ the factor(which will be in the even power, say $P^2$) of the form $(2^r\prod q_i)^2$ where $q_i≡-1(mod\ 4)$ will have only one form as the sum of squares $0^2+P^2$.
Any number $N$ having $m$ prime factors each ≡1(mod 4) can be expressed $2^{m-1}$ ways.
If ${2u}$ is the highest power of $p_i$ that divides $n^2$ where $p_i$ ≡1(mod 4),
the number possible ways of $p_i^{2u}$ can be expressed as a sum of squares is
$u$ if we only allow positive integers,
$2u$ if we allow non-zero integers,
$2u+1$ (1 due to $p_i^{2u}+0^2$) if we allow all integers .
If $n^2=(2^r\prod q_i)^2\prod_{1 ≤i ≤ n} p_i^{2u_i}$, the number possible ways will be $\prod (2u_i+1)$
Look into this, Theorem 3 for details.